Execution order between Awake(), yield xxx, start()

Continuing the discussion from Null reference issues when returning to first scene:
@Brian_Trotter Hi,i don’t understand why you said "After this line of code, all Start() methods are run…"Shouldn’t start() start executing after running all the code in Transition()?
Maybe I’m still not very clear about the order of execution between Awake(), yield xxx, start().

The answer is no. The transition is a coroutine which runs over multiple frames. The function essentially ends when it gets to the yield, allowing other code that may normally wait for a function to complete to run. So, all the starts run while the transition routine is waiting for x seconds to pass.

1 Like

That is to say, after yield return SceneManager.LoadSceneAsync, all objects in the scene are loaded, and then the scripts of the respective objects begin to execute Awake(), start() in order, and the original Transition() script will be blocked and wait because of yield return new WaitForSeconds and other operations, Transition( ) will not affect the execution of Awake() and start() of other scripts.
Is that so?

When loading a scene, load an object and that object directly start sequential execution of Awake(), start() or wait until yield return SceneManager.LoadSceneAsync ends and then all objects start executing Awake(), start() sequentially?

When

yield return SceneManager.LoadSceneAsync();

returns, all components within the scene will have had their Awake() methods completed.
OnEnable(), Start(), and Update() on all these components will be executed the next time the Coroutine encounters a yield return statement.

1 Like

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

Privacy & Terms