DontDestroyOnLoad - I dont want the parent

Hey

If I use Object.DontDestroyOnLoad, how can I keep an object that is child of another object which I dont want to keep?
If I dont persist the parent object, I will get the error in the next scene that I try to access an object which has been deleted.

Parent

  • Child

I want to keep Child, but it seems (Unity 4.6) that I need to DontDestroyOnLoad(Parent). Am I misusing it? And if I HAVE to keep the Parent object, how can I delete it from the new scene once it has been loaded and just keep the Child?

thx in advance

If you need to keep only the children on a new scene, but the parent should be destroyed, use the OnDestroy() method to set the children’s parent attributes to null (or better yet, reparent to whichever object should manage these across scenes) and call DontDestroyOnLoad() on them instead.

1 Like