DontDestroyOnLoad Child Object

I’ve tested this out and it seems to be the case, but I wanted confirmation.

I have two objects, Parent and Child. Child is a child of Parent in the scene hierarchy.
Child has a script on it, that calls the following in its Start():

	DontDestroyOnLoad(gameObject);

And then changes scene.

In the new scene, Child is not there.

It seems to be if the object with DontDestroyOnLoad is a child of something, the DontDestroyOnLoad() call is not respected.

Is this correct, expected behaviour? I was expecting it to appear in the new scene without any parent.
It’s going to be a real pain to move it out from under its parent before the scene changes.

This is the correct behaviour. Only direct scene objects are saved, so you should unparent your child object before you load the new scene. You can simply do this by using BroadcastMessage on the parent object. Just have your script on the child react to the message and unparent itself.

It would be great if there was a OnLevelLoading which would be called before the level loads in contrast to OnLevelWasLoaded, but there is no such function. What “might” also work is to unparent the object in OnDisable, but it could already be too late.

Yes. When the engine destroy all the object in the current scene to move to the next he checks each object (in the first hierarchy level) so if your parent doesn’t have

  DontDestroyOnLoad(gameObject);

it will be destroy and he will not check its children even if these ones have “DontDestroyOnLoad”

==> You can read a previous Bunny’s answer here :wink: