- Home /
Are GameObjects "awoken" in the same order if I load the same Scene twice ?
Hi everyone,
I've been discovering Unity for a few months, and now I feel I can ask questions. ;)
We all know that the order in which GameObjects are "awoken" is unspecified (I prefer to say unspecified rather than random). Now, let's say I load the same Scene twice, without modifying it between the two loads. Can I at least assume the GameObjects will be "awoken" in the same order ?
Thanks in advance !
Since at least after the game was build and everything is YA$$anonymous$$Led and whatnot, I would say, yes, the execution order is the same, but...
even if it was the same the second time, you shouldn't program anything relying on that assumption. You as a programmer should always know when you need control over execution order and try to get it. Unity's Script Execution Order lets you specify that for the cases you need it.
Thanks for your answer.
To tell you the truth, I was hoping I could use that to easily generate scene-unique IDs for my GameObjects (for a checkpoint system). But as it does not sound reasonable engouh, then I guess I'll just use one of the many other solutions listed in these forums.
Can you not use https://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html for your uniqueIDs? I don't know enough about what exactly you need, but that has worked well for me.
Alternatively, you can generate IDs on your objects after they have all awoken by using some external manager class and looping over them.
Well, I assume the IDs given by GetInstanceID to be different if I reload the Scene, and I need them to be the same (I reload the Scene before restoring the checkpoint data).
Anyways, my current solution to generate a unique ID is to use a combination of the object's Transform and name (I've read this answer several times on UA). That's sufficient my project, but I was hoping I could do better by giving the IDs sequentially in Awake (that was assu$$anonymous$$g the objects would always be awoken in the same order).