Doubt with DontDestroyOnLoad()

I’m having some doubt with DontDestroyOnLoad() :-
We first use an if statement to check weather their are more than one gameStatus gameobject or not in Level-2 and if their are, then we write Destroy(gameobject) to destroy the extra one…However this same script is on the gameStatus gameobject of Level-1(which came to Level-2 by DontDestroyOnLoad()) and and that of Level-2, so why dosen’t both of them gets destroyed??

[ Sorry if I write something dumb, Simpleton is not that simple for me :sweat_smile: ]

Also why do we need to write this whole code in Awake(), why not in Start() ??

Hi Abhinav,

That’s a very good question. Thank you for asking.

The secret of this solution is that Awake and Start get called only once during the lifetime of an instance/object. Our persistent GameStatus object which was created in Level 1, does not have its Awake or Start method called again in Level 2. For this reason, its self-destruction code would never get executed.

We put the code in Awake because Awake gets called before Start. If other instances depend on our persistent GameStatus and look for the reference in Start, we don’t risk that they find the wrong GameStatus object, meaning one which gets destroyed in Awake. The order of method calls is arbitrary in Unity. We do not know if Awake of game object 1 or Awake of game object 2 gets called first. What we know is: All Awake methods get called first, then all Start methods get called.

Does that make sense?


See also:

1 Like

Singleton : All levels that want to share the gamestatus have a gamestatus object with the dontdestroy etc code in them and when the first level of the game loads eg. level 1 - the gamestatus object of that first level is the only one in existence and so wont be destroyed as per the logic we have setup in Awake.
Now later on when the player gets to level 2 or 3 etc that new level will see 2 instances of gamestatus . The one loaded in level 1 and the one loaded up in the new level but since as per our check we destroy the gamestatus object if we find a previous one already exists , the gamestatus on the new level ie. level 2 will destroy itself. This repeats on level 3 , 4 and so on.
So effectively this means that the gamestatus object from level 1 will be the only one in existence throughout the game allowing all levels to share it and use it making it the singleton in use throughout.

Awake and Start are both called only once per gameobject but Awake is called before Start and is a recommended place to run initialisation logic. Also since the Awakes of all objects are called before any of the Starts , by doing the singleton checking etc in Awake we can be sure we have the gamestatus object in the current level setup correctly before we try to use it in the Start function of some other object that wants to use it.

2 Likes

Thnx…I got it :+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms