0
0
Unityframework~10 mins

Coroutine basics (IEnumerator) in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Coroutine basics (IEnumerator)
Start Coroutine
Execute IEnumerator Step
Yield return Wait or null?
Pause Execution
Check if more steps
Yes
Execute Next IEnumerator Step
No
Coroutine Ends
The coroutine starts and runs each step of the IEnumerator. When it hits a yield, it pauses and resumes later until all steps finish.
Execution Sample
Unity
IEnumerator ExampleCoroutine() {
  Debug.Log("Start");
  yield return null;
  Debug.Log("After one frame");
  yield return new WaitForSeconds(1);
  Debug.Log("After 1 second");
}
This coroutine logs messages, waits one frame, then waits one second before finishing.
Execution Table
StepActionYield ReturnedCoroutine Paused?Output
1Start coroutine, run first Debug.LognullYes (wait one frame)Start
2Resume after one frame, run second Debug.LogWaitForSeconds(1)Yes (wait 1 second)After one frame
3Resume after 1 second, run third Debug.LognoneNo (coroutine ends)After 1 second
💡 Coroutine ends after all steps run and no more yield statements.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Coroutine StateNot startedPaused (yield null)Paused (yield WaitForSeconds)Finished
Key Moments - 3 Insights
Why does the coroutine pause after 'yield return null'?
Because 'yield return null' tells Unity to pause the coroutine and resume it in the next frame, as shown in execution_table step 1.
What happens when the coroutine yields 'WaitForSeconds(1)'?
The coroutine pauses for 1 second before continuing, as seen in execution_table step 2 where it waits before running the next Debug.Log.
When does the coroutine actually end?
It ends after the last Debug.Log runs and there is no more yield, shown in execution_table step 3 where 'Coroutine Paused?' is No.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the coroutine state after step 2?
AFinished
BPaused waiting for 1 second
CRunning without pause
DPaused waiting for next frame
💡 Hint
Check 'Coroutine Paused?' and 'Yield Returned' columns in execution_table row 2.
At which step does the coroutine stop pausing and finish?
AStep 3
BStep 2
CStep 1
DIt never finishes
💡 Hint
Look at 'Coroutine Paused?' column in execution_table to find when it says 'No'.
If we remove 'yield return null', what changes in the execution_table?
ACoroutine pauses for one frame at step 1
BCoroutine pauses for 1 second at step 1
CFirst two Debug.Logs run immediately, then pause for 1 second
DCoroutine never starts
💡 Hint
Refer to how 'yield return null' causes pause in execution_table step 1.
Concept Snapshot
Coroutine basics with IEnumerator in Unity:
- Coroutine runs code step-by-step.
- 'yield return null' pauses until next frame.
- 'yield return WaitForSeconds(x)' pauses for x seconds.
- Coroutine resumes after each yield until done.
- Use Debug.Log to see execution order.
Full Transcript
This visual trace shows how a Unity coroutine using IEnumerator runs step-by-step. The coroutine starts and runs the first Debug.Log, then pauses because of 'yield return null' until the next frame. After resuming, it logs again and pauses for 1 second due to 'yield return new WaitForSeconds(1)'. Finally, it logs the last message and ends because there are no more yield statements. Variables track the coroutine state as Not started, Paused, and Finished. Key moments clarify why the coroutine pauses and when it ends. The quiz tests understanding of pause points and coroutine flow.