DontDestroyOnLoad and Awake

Hey everyone.

It is (was) my understanding that Awake is only called once on each game object for that entire GameObjects lifecycle. However, after calling DontDestroyOnLoad on a game object and then loading a different scene, that persisted GameObjects Awake method is called again in the second scene which caused me a few bugs.

So am I correct in thinking that Awake/Start etc are tied to the lifecycle of the scene even if they are persisted between scenes?

1 Like

That doesn’t sound right. If a script’s gameObject has been marked as DontDestroyOnLoad it won’t fire Awake when a new scene has loaded.

Have you tried reproducing this in a brand new project? You’re probably causing it to re-fire from somewhere else in your existing project.

I take it the object/script is only in the first scene? And not defined in the second scene being loaded at all?

1 Like

Yeah I thought so myself, I guessed it was something I had done… And as you predicted, nested deep (5 layers down) in game object tree there are was a another of those components sitting there… I have no idea how that got there…

Put this one down to me being stupid with copy and paste.

I will now headbutt my keyboard as penance.

Thanks for the help!

1 Like

Yeah, having script components in some weird location can be sneaky.

What I often do is right click on a script in the Project pane and choose ‘Find References in Scene’ to show all Gameobjects or dependencies on that script.

1 Like