- Home /
Gamecontroller "Awake" instantiates other objects - Event order question
I load a scene where the only object is my game controller. During its Awake() it loads some saved data and instantiates a lot of other objects in their saved locations.
These instantiated objects have to do some self-setting up in their Awake() and after that the Gamecontroller must do something for each of them based on the values the objects have set up for themselves, which I was hoping to put in the controller's Start()
Given that the saved objects are instantiated during the controller's awake(), and the self-setting up is done during the saved objects' awake(), can I be reassured that all this will be completed before the controller gets to doing its own Start()?
Answer by pako · Nov 23, 2017 at 07:54 PM
@Ingejones you say that:
During its Awake() it loads some saved data and instantiates a lot of other objects
I haven't seen your code, but it seems that before the game controller's Awake()
completes, the saved objects will be instantiated. When they get intantiated (apparently in an "Active" state), their Awake()
will run to completion, before game controller's Awake()
completes. This also means that the saved objects' OnEnable()
will also run immediately after each object's Awake()
, and after all these Awake()/OnEnable()
sequences, the game controller's Awake()
will complete, followed by its OnEnable()
and then, before the first Update()
all Start()
methods will run. Of course, if you don't have any code in any OnEnable()
there's nothing to run there.
To put it simply, the game controller's Awake()
cannot complete before all instantiations made inside this Awake()
complete, and theses instantiations will only complete, after all relative Awake()s
complete. So, yes, I assure you that all of the saved objects Awake()
methods will be completed, before the controller's Start()
. It's very easy to test this yourself. Just put in each script's Awake()
and Start()
Debug.Log statements e.g.:
void Awake(){
//At the start of the method
Debug.Log(name + " - Awake() Started.");
//code...
//At the end of the method
Debug.Log(name + " - Awake() Completed.");
}
void Start(){
//At the start of the method
Debug.Log(name + " - Start() Started.");
//code...
//At the end of the method
Debug.Log(name + " - Start() Completed.");
}
Thanks @pako. I did in fact test with debug logging and got the results as you say, but I just wanted reassurance that it was a predictable result and not just some lucky coincidence related to this particular context, that everything was showing up in the desired order. I had read so many references to "indeter$$anonymous$$ate order" etc that I didn't want to base my entire plans on something that might suddenly up-end itself!
Answer by jeffreyrampineda · Nov 23, 2017 at 04:24 PM
It says on the reference that "Awake is always called before any Start functions" https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html
Yes for objects that are already on the scene in editor. However in my case all but one of these are instantiated at runtime, during the original object's Awake(). The manual, which I did read before posting there, says "Note that for objects added to the scene, the Awake and OnEnable functions for all scripts will be called before Start, Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay." and of course instantiating post-editor, during the loading of a save, is theoretically in gameplay. So I am asking about my specific case hoping that someone can address that.