Start, Awake, Update and StartCoroutine in Unity.
In Unity, Coroutines leverage C#'s IEnumerator/yield syntax feature to easily let you write stepwise behaviors that execute over a longer amount of time/over multiple update cycles within just one method.
But when exactly do they get executed?
Of course, some of it is documented in their classic manual page: Order of Execution (2021.3)
But there's a strange thing I didn't really think of when I started using Coroutines so I thought I'd document it here.
When does the first step of a coroutine execute? And if I started a coroutine in Awake, and another in Start, would the code after yield null execute sequentially after the next Update?
The answer may not be what you expect.
Here's some code to test:
Stick it on a GameObject, run the scene and let's see what we got.
huh...
A few notable observations:
Stuff before the first yield null execute immediately as if they were where the StartCoroutine call is.
Using StartCoroutine in Start seems to make the first yield null skip one Update. This kind of makes practical sense, since the common intention of a yield return null is you want it to wait for one update. If it happened in the first update after Start, it would be as if it didn't wait at all.
So really, the thing to remember is: Stuff after the first yield null in Awake executes right after the first update. Stuff after the first yield null in Start executes right after the second update.












