The Limitation of Coroutines: A Structure That Cannot Pause
Unity's coroutine is a feature that allows for simple flow control. However, it fundamentally supports only "start" and "complete stop." It does not offer the ability to pause and resume execution midway.
Why Coroutines Cannot Be Recovered
Coroutines created with StartCoroutine are managed internally in memory as a "Coroutine Handle." When StopCoroutine or StopAllCoroutines is called, this handle is completely removed from memory. Once deleted, any information about how far the coroutine had progressed is lost. Therefore, it is impossible to resume a coroutine once it has been stopped.
A Structure That Cannot Save Progress
Coroutines do not save their progress internally. If you call StopCoroutine during execution, it will immediately terminate, and if you later call StartCoroutine again, it will always start from the beginning. To resume from a midpoint, you would need to manually save the progress externally and branch your logic accordingly. However, doing so significantly increases code complexity and makes maintenance harder.
Example of Coroutine Behavior
IEnumerator SomeCoroutine()
{
Debug.Log("Start");
yield return new WaitForSeconds(1f);
Debug.Log("After 1 second");
yield return new WaitForSeconds(2f);
Debug.Log("After 3 seconds total");
}
- StartCoroutine(SomeCoroutine()) is called
- "Start" is printed
- After 1 second, "After 1 second" is printed
- After another 2 seconds, "After 3 seconds total" is printed
If StopCoroutine is called midway, the coroutine immediately terminates, and the progress is not saved. Restarting StartCoroutine will execute from the very beginning again.
Simple Flow Summary
StartCoroutine() → Coroutine Handle created → stays alive in memory → proceeds frame by frame
StopCoroutine() → Coroutine Handle removed → deleted from memory → finished
When Coroutines Are Suitable and When They Are Not
- Coroutines are very suitable for simple flow control, such as effect execution and short timers.
- They are not suitable for cases requiring game pause/resume, save/load systems, or complex game progress management.
- In such cases, using Update functions and a state machine (FSM) for explicit control is much more stable and manageable.
Conclusion
Unity's coroutine system is very useful for implementing sequential flows simply. However, when building systems that require mid-execution pauses, saving, or restoring, the limitations of coroutines become evident. When using coroutines, it is important to understand these characteristics and design your implementation accordingly.