Hi, I've placed a script on a spawned object that doesn't work. I've used the script to detect colliders with a ball prefab from the hierarchy and it works perfectly, but when I try the same script on the same prefab that spawns in the game it doesn't recognize the colliders. The ball script detects the colliders by recognizing the collider's tag. I've tried using the prefab from the project window instead of the hierarchy for the spawned ball, but that doesn't work either. Is there a reason why the script wouldn't work on a spawned object?
I don't understand your problem. How do you place a script on a spawned object? Do you attach the Script on runtime? I think you should post some parts of your code and maybe try to explain your problem different.
Thanks for getting back to me, I'll try to explain it better. I've attached a script to a ball prefab in the assets folder that detects a collider (the specific tag associated with the collider). When the ball is spawned in the game it doesn't trigger the plane to tilt when the ball comes in contact with the collider. However, when I place the same script on a ball prefab from the Hierarchy it is able to detect the colliders and makes the plane tilt. Here is the detect script I'm using: Code (CSharp): public class DetectWall : MonoBehaviour { public GameObject Platform; //Level GameObject that gets rotated private RotatePlane rPlane; //Level GameObject's script to access rotation coroutines public GameObject CamAnchor; //The Empty Game Object the Camera is parented under, to be a pivot for rotation private CamRotate camRot; //Camera Pivot's script to access rotation coroutines public float thrust; //To push the ball, how fast you move it private Rigidbody rb; //The ball's rigidbody private Coroutine rotCoroutine; //platform rotation coroutine being used private Coroutine camCoroutine; //camera rotation coroutine being used void Start() { rPlane = Platform.GetComponent<RotatePlane> (); //Get the script from the platform camRot = CamAnchor.GetComponent<CamRotate>(); //Get the script from the cam Pivot/Anchor rb = GetComponent<Rigidbody> (); //Get the ball's rigidbody for pushing it manually } void OnCollisionEnter(Collision col) { if(col.gameObject.tag == "TopWall") //If the collider is tagged as "TopWall" { if (rotCoroutine != null) { //Stops old platform rotation coroutine (if there is one). This is because the two seperate slerps at once can make it wonky StopCoroutine (rotCoroutine); } if (camCoroutine != null) { //Stops old camera rotation coroutine (if there is one) StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.TopWall ()); //Starts the plane's TopWall rotation coroutine. camCoroutine = StartCoroutine (camRot.TopMove ()); //Starts the camera's TopRot rotation coroutine. } if(col.gameObject.tag == "BottomWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.BottomWall ()); camCoroutine = StartCoroutine (camRot.BottomMove ()); } if(col.gameObject.tag == "LeftWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.LeftWall ()); camCoroutine = StartCoroutine (camRot.LeftMove ()); } if(col.gameObject.tag == "RightWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.RightWall ()); camCoroutine = StartCoroutine (camRot.RightMove ()); } if (col.gameObject.tag == "Flatten") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.BaseWall ()); camCoroutine = StartCoroutine (camRot.EvenCamera ()); } } void OnTriggerEnter(Collider col) { if(col.gameObject.tag == "TopWall") //If the collider is tagged as "TopWall" { if (rotCoroutine != null) { //Stops old platform rotation coroutine (if there is one). This is because the two seperate slerps at once can make it wonky StopCoroutine (rotCoroutine); } if (camCoroutine != null) { //Stops old camera rotation coroutine (if there is one) StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.TopWall ()); //Starts the plane's TopWall rotation coroutine. camCoroutine = StartCoroutine (camRot.TopMove ()); //Starts the camera's TopRot rotation coroutine. } if(col.gameObject.tag == "BottomWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.BottomWall ()); camCoroutine = StartCoroutine (camRot.BottomMove ()); } if(col.gameObject.tag == "LeftWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.LeftWall ()); camCoroutine = StartCoroutine (camRot.LeftMove ()); } if(col.gameObject.tag == "RightWall") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.RightWall ()); camCoroutine = StartCoroutine (camRot.RightMove ()); } if (col.gameObject.tag == "Flatten") { if (rotCoroutine != null) { StopCoroutine (rotCoroutine); } if (camCoroutine != null) { StopCoroutine (camCoroutine); } rotCoroutine = StartCoroutine (rPlane.BaseWall ()); camCoroutine = StartCoroutine (camRot.EvenCamera ()); } } void Update() { //Controls to move the ball (for testing) if (Input.GetKey (KeyCode.W)) { rb.AddForce (0, 0, thrust); } if (Input.GetKey (KeyCode.S)) { rb.AddForce (0, 0, thrust * -1); } if (Input.GetKey (KeyCode.A)) { rb.AddForce (thrust * -1, 0, 0); } if (Input.GetKey (KeyCode.D)) { rb.AddForce (thrust, 0, 0); } } }
Here is the ball script that was added to it before I added my script (it's somebody else's javascript): Code (JavaScript): // Ball.js : Description : this script manage the ball (sound, collision, trail) #pragma strict private var rb : Rigidbody; // Ball RigidBody Component private var trail : TrailRenderer; // Ball TrailRenderer Component @Header ("The maximum speed of the ball") var maxSpeed : float = 25; // change the ball Max speed on table. If you change the cabinet scale you probably must increase the Max speed @Header ("Trail") var Speed_To_Activate_Trail : float = .5; // The Trail become active when ball speed is superior to Speed_To_Activate_Trail private var b_trail : boolean = true; private var tmp_vel : Vector3; // Used with the function Ball_Pause() @Header ("Roll Sound") private var roll_audio : AudioSource; // roll AudioSource Component private var once : boolean = false; // Boolean used to play roll sound only if the sound is not playing var min_Mag_roll_audio : float = 1; // The minimum speed to play the roll sound private var tmp_Save_Min_Mag : float = 0; var b_Shake : boolean = false; var Shake_Force : float = 2; private var b_OnHole : boolean = false; // Use to know if ball is on a hole or not function Start() { // --> function Start rb = GetComponent.<Rigidbody>(); // Access <Rigidbody>() Component; trail = GetComponent.<TrailRenderer>(); // Access <TrailRenderer>() Component; roll_audio = GetComponent.<AudioSource>(); // Access <AudioSource>() Component; if roll sound is selected on the inspector } function Ball_Shake(Direction : Vector3){ if(rb)rb.AddForce(Direction*Shake_Force, ForceMode.VelocityChange); } function FixedUpdate() // --> Fixed Update : FixedUpdate is used to deal with Physics { if(rb.velocity.magnitude > maxSpeed) // Limit ball speed. { rb.velocity = rb.velocity.normalized * maxSpeed; } if(b_trail && rb.velocity.magnitude > Speed_To_Activate_Trail) // Enable ball trail. { trail.enabled=true; b_trail = false; } if(!b_trail && rb.velocity.magnitude < Speed_To_Activate_Trail) // Desable ball trail. { trail.enabled=false; b_trail = true; } if(rb.velocity.magnitude > min_Mag_roll_audio && once) // Play the roll sound. { roll_audio.Play(); once = false; } else if(rb.velocity.magnitude <= min_Mag_roll_audio && !once) // Stop the roll sound. { roll_audio.Stop(); roll_audio.pitch = 1; once = true; } if(!once && tmp_Save_Min_Mag == 0){ roll_audio.pitch = rb.velocity.magnitude/2.5; // When ball accelerate the pitch increase. } } function Ball_Pause(){ // --> Function Call When the game is on pause if(!b_OnHole){ if(!rb.isKinematic){ // Start Pause tmp_vel = rb.velocity; rb.isKinematic = true; } else{ // Stop Pause rb.isKinematic = false; rb.velocity = tmp_vel; } } } function OnHole(){ // --> Know if ball is on a hole. Prevent bug when a ball enter a hole and player press pause. b_OnHole = true; } function OutsideHole(){ b_OnHole = false; } function OnTriggerEnter(other : Collider){ if(other.transform.tag == "Ramp_Sound" && tmp_Save_Min_Mag == 0){ Debug.Log("Ramp SOund Start"); tmp_Save_Min_Mag = min_Mag_roll_audio; min_Mag_roll_audio = 0; } else if(other.transform.tag == "Ramp_Sound"){ Debug.Log("Ramp SOund Stop "); min_Mag_roll_audio = tmp_Save_Min_Mag; tmp_Save_Min_Mag = 0; } }