![]() |
![]() |
|
![]() |
||||||||||
When writing game code, one often ends up needing to script a sequence of events. This could result in code like the following.
It is often more convenient to use the yield statement. The yield statement is a special kind of return, that ensures that the function will continue from the line after the yield statement next time it is called.
You can also pass special values to the yield statement to delay the execution of the Update function until a certain event has occurred.
You can both stack and chain coroutines.
This example will execute Do but continue after calling do immediately.
This example will execute Do and wait until it is finished before continuing its own execution
Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can.
See YieldInstruction, WaitForSeconds, WaitForFixedUpdate, Coroutine and MonoBehaviour.StartCoroutine for more information on using yield.