- Home /
Destroyed prefab still moving
Hello. I am doing the example Space Shooter from tutorials and using Unity 4.3.4f1. According to the tutorial, clones should disappear from hierarchy view but mine is still there, only the triangle on the left disappears. But I see that the Z position of the clone is increasing, so I guess the object doesn't stop. Is this expected?
I think I did everything same as tutorial. This is the code from tutorial:
public class PlayerController : MonoBehaviour
{
...
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
...
}
public class DestroyByBoundary : MonoBehaviour {
void OnTriggerExit (Collider other)
{
Destroy(other.gameObject);
}
}
I'm not familiar with that tutorial, but a common mistake is to call Destroy on a component (which removes the component, but not its GameObject). Similarly, and what I might guess is happening here: if your prefab is a collection of nested/parented GameObjects, you'll need to destroy all of them (usually by destroying the parent).
Answer by AaronWinterhoff · Feb 04, 2014 at 11:52 PM
Hi. Try putting the DestroyByBoundary script on the same gameObject as the one that has the PlayerController script.
The Destroy ( other.gameObject ), function is destroying whichever gameObject your collider is on. I haven't seen the tutorial, but it's likely that this DestroyByBoundary script needs to be on the top level gameObject, so that the object deleted includes the entire hierarchy.
You will need a collider on the top level, but I'm guessing that there will be one there. It's likely this is the same object that the PlayerController script will be on.
To explain, if you have the script on a lower element, like the 'sphere' in my screen shot, it will only delete the sphere. If you have it on the SpaceShip (highlighted blue), then everything will be destroyed.
Once you have an answer you're happy with (doesn't have to be this one), please remember to upvote it and tick it as an accepted answer, to help others who might have a similar problem).
Thank you for answers! I solved it! I instantiated a shot with the tutorial's Bolt object ins$$anonymous$$d $$anonymous$$e and it worked. I compared $$anonymous$$e with tutorial's. 'shot' parameter is type of Bolt Object and Bolt has VFX object under it. Reason was, my Bolt object had Rigid Body but no Capsule Collider, while VFX had Capsule Collider without Rigid Body. I remember something like, for colliders to work, rigid body must be added.
Your answer
Follow this Question
Related Questions
How to destroy all gameobjects active in hierarchy? 1 Answer
why is OnTriggerEnter Destroy working once then stoping? 0 Answers
'Picking up Ball' by destroying Prefab, prefab does not get 'read' 1 Answer
Is there a script that will trigger a gui texture when an object is destroyed? 2 Answers
Sword Kills Enemies From Far Away 3 Answers