- Home /
Clones are still in the hierarchy after exiting play mode
Is this intentional? If not, im Instantaiting a couple of 2D Toolkit Heart Sprites, and parenting them to the camera. For some reason when i exit out of play mode they dont get removed.
http://pastebin.com/Wa9PDgB5 Heres the code for the instantiating, any help is appreciated!
Answer by supernat · Dec 20, 2013 at 06:02 AM
No, it should not be doing that. Some plugins will do this by monitoring your hierarchy and re-creating the objects after you leave Play mode, but they are specifically designed to do this to save new objects created at runtime. Do you have any plugins that you are using?
I'm using 2D Toolkit and Unity Gems Finite State $$anonymous$$achine, I'm assu$$anonymous$$g it might have something to do with 2D Toolkit but I'm not sure.
Answer by dkydev · Jul 25, 2018 at 06:11 PM
Old question but I just ran into this issue myself in 2018.1:
Seems like it is caused by setting the instantiated gameobject's parent transform directly - after instantiation. From your example:
heart.transform.parent = camera.transform;
Instead use Object.Instantiate and specify the parent transform as the second parameter:
GameObject heart = GameObject.Instantiate(healthSpritePrefab, camera.transform);
heart.transform.position = position;
And the cloned gameobjects will be deleted when you exit playmode.
I haven't found any documentation yet as to why this is the case. If anyone knows please share. Thanks!
I encountered this issue too and used your solution but sadly it didn't work for me.
I instaniate an UI element and create it as a child of an existing UI element in the scene. Afterwards I instantiate more UI elements and set them as childrien from the first instantiated one. Neither the first form you show heart.transform.parent = camera.transform;
or the second one ameObject heart = GameObject.Instantiate(healthSpritePrefab, camera.transform);
works -.-