- Home /
Will DontdestroyonLoad() Keep Child Objects too
If I have a GameObject named Manager that has a Public GameObject() variable in an attached script, and I then drag a GameObject named Player to attach it to that script attach point in the Unity UI, if Don'tDestroyOnLoad() is called on the Manager object, will the player object also not be destroyed when I switch to scene 2?
Answer by fafase · Aug 17, 2013 at 10:02 AM
How about trying it? or simply reading the docs:
If the object is a component or game object then its entire transform hierarchy will not be destroyed either.
Answer by gsalmon · Mar 30, 2015 at 05:51 AM
Correct, child objects within the objects hierarchy are not destroyed:
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Note that objects must be at the top-level in order for DontDestroyOnload to preserve them: Calling DontDestroyOnload on an object that is a child of another object will not work.
One thing to consider though is that if you move a children from one parent with DontDestroyOnLoad to the root (SetParent(null)) the object keeps the DontDestroyOnLoad too.
Not sure if that is the correct behaviour but, at least on Editor, it behaves like that.