- Home /
Destroying a prefab doesn't remove it from the Hierarchy list
Hi all.
I create a prefab like this: baddie = Instantiate(Resources.Load("Alien"));
The baddie has a script and when he dies I call Destroy(gameObject) from within the alien script. This removes him from screen but doesnt remove him from the Hierarchy list which he was added to when I created him. Any ideas guys? Oh he's called Alien(Clone) in the Hierarchy.
"I create a prefab like this: baddie = Instantiate(Resources.Load("Alien"));"
Actually you're creating a GameObject, using a prefab as a template. Prefabs are abstract and never exist in the game world.
Answer by flaviusxvii · Jun 22, 2011 at 02:48 PM
Destroy should always work. Are you sure you're not calling it on some sub-object of the Alien?
I call Destroy(gameObject) from within the script. Does this mean I'm only destoying the script? How do I get at the root prefab from within the script?
Please use the comments section or 'Edit' for follow up questions, rather than 'Answer'.
Answer by _Petroz · Jun 23, 2011 at 02:22 AM
I think you're misunderstanding the difference between a Prefab and a GameObject. You can clone a GameObject using Instantiate, you can then destroy that clone from inside scripts attached to the cloned using destroy(gameObject). If you want to destroy the original object which was used to clone the new object you have to find it.
http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
Your answer
Follow this Question
Related Questions
How to destroy all gameobjects active in hierarchy? 1 Answer
GetChild(0) returns nonexistent object 1 Answer
when prefabs are spawned I want to touch them to destroy. help? 0 Answers
Destroying all enemy characters after the level is over 4 Answers
how to destroy an object then instantiate another from a prefab 1 Answer