0
0
Unityframework~10 mins

Stopping coroutines in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stopping coroutines
Start Coroutine
Coroutine Running
Check Stop Condition?
YesStop Coroutine
No
Continue Coroutine
Coroutine Ends Normally
This flow shows how a coroutine starts, runs, checks if it should stop, and either continues or stops.
Execution Sample
Unity
IEnumerator MyCoroutine() {
  while(true) {
    yield return null;
  }
}

Coroutine myCor = StartCoroutine(MyCoroutine());
StopCoroutine(myCor);
Starts a coroutine that runs indefinitely and then stops it.
Execution Table
StepActionCoroutine StateResult
1StartCoroutine(MyCoroutine()) calledRunningCoroutine begins execution
2Coroutine yields null (waits one frame)RunningCoroutine pauses, will resume next frame
3Coroutine resumes next frameRunningCoroutine continues loop
4StopCoroutine(myCor) calledStoppingCoroutine is marked to stop
5Coroutine stops before next yieldStoppedCoroutine execution ends
6Attempt to resume coroutineStoppedNo action, coroutine already stopped
💡 Coroutine stops when StopCoroutine is called, preventing further execution.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Coroutine StateNot startedRunningStoppingStopped
Key Moments - 2 Insights
Why does the coroutine stop immediately after StopCoroutine is called?
Because StopCoroutine marks the coroutine to stop, so it does not continue to the next yield or loop iteration as shown in steps 4 and 5 of the execution_table.
Can you stop a coroutine by calling StopCoroutine with the method name directly?
No, you must keep a reference to the started coroutine or use the IEnumerator instance; calling StopCoroutine with the method name alone may not stop the running coroutine properly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the coroutine state after step 2?
ARunning
BStopped
CNot started
DStopping
💡 Hint
Check the 'Coroutine State' column at step 2 in the execution_table.
At which step does the coroutine stop execution?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for when the 'Coroutine State' changes to 'Stopped' in the execution_table.
If StopCoroutine was never called, what would happen at step 6?
ACoroutine would stop automatically
BCoroutine would continue running
CCoroutine would throw an error
DCoroutine would pause indefinitely
💡 Hint
Refer to the 'Coroutine State' progression in variable_tracker and execution_table.
Concept Snapshot
Stopping coroutines in Unity:
- Use StartCoroutine to begin.
- Coroutine runs until it yields or ends.
- Call StopCoroutine with the coroutine reference to stop it.
- Stopped coroutine does not resume.
- Keep reference to coroutine for reliable stopping.
Full Transcript
This visual trace shows how a coroutine in Unity starts running, yields control, and can be stopped by calling StopCoroutine. Initially, the coroutine is running and yields null to wait one frame. When StopCoroutine is called, the coroutine state changes to stopping and then stopped, preventing further execution. Variables track the coroutine state from not started to running, stopping, and finally stopped. Key moments clarify that StopCoroutine immediately halts the coroutine and that you need a reference to stop it properly. The quiz tests understanding of coroutine states at different steps and the effect of stopping or not stopping the coroutine.