Challenge - 5 Problems
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when stopping a coroutine by name?
Consider the following Unity C# code snippet. What will be printed to the console when the coroutine is stopped by name?
Unity
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { IEnumerator MyCoroutine() { Debug.Log("Start"); yield return new WaitForSeconds(1); Debug.Log("End"); } void Start() { StartCoroutine("MyCoroutine"); StopCoroutine("MyCoroutine"); } }
Attempts:
2 left
💡 Hint
Stopping a coroutine by name immediately stops it before it can continue.
✗ Incorrect
The coroutine starts and prints "Start" immediately, but StopCoroutine("MyCoroutine") stops it before the yield completes, so "End" is never printed.
❓ Predict Output
intermediate2:00remaining
What happens if you stop a coroutine using its IEnumerator reference?
Look at this Unity C# code. What will be the output when the coroutine is stopped using its IEnumerator reference?
Unity
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { IEnumerator coroutineRef; IEnumerator MyCoroutine() { Debug.Log("Running"); yield return new WaitForSeconds(1); Debug.Log("Finished"); } void Start() { coroutineRef = MyCoroutine(); StartCoroutine(coroutineRef); StopCoroutine(coroutineRef); } }
Attempts:
2 left
💡 Hint
Stopping a coroutine by IEnumerator reference stops it immediately after the first yield.
✗ Incorrect
The first Debug.Log("Running") executes before the yield, so it prints. But StopCoroutine stops the coroutine before it can print "Finished".
🔧 Debug
advanced2:30remaining
Why does this coroutine not stop as expected?
This code tries to stop a coroutine but it keeps running. What is the reason?
Unity
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { IEnumerator coroutineRef; IEnumerator MyCoroutine() { while(true) { Debug.Log("Looping"); yield return new WaitForSeconds(1); } } void Start() { coroutineRef = MyCoroutine(); StartCoroutine(coroutineRef); StopCoroutine(MyCoroutine()); } }
Attempts:
2 left
💡 Hint
Check how the coroutine reference is passed to StopCoroutine.
✗ Incorrect
StopCoroutine(MyCoroutine()) creates a new IEnumerator different from the one running, so it does not stop the running coroutine.
🧠 Conceptual
advanced1:30remaining
What happens when you call StopAllCoroutines()?
In Unity, what is the effect of calling StopAllCoroutines() inside a MonoBehaviour?
Attempts:
2 left
💡 Hint
Think about the scope of coroutines in Unity.
✗ Incorrect
StopAllCoroutines stops all coroutines started by the current MonoBehaviour instance only, not coroutines on other objects.
❓ Predict Output
expert3:00remaining
What is the output after stopping a nested coroutine?
Analyze this Unity C# code. What will be printed to the console?
Unity
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { IEnumerator InnerCoroutine() { Debug.Log("Inner Start"); yield return new WaitForSeconds(1); Debug.Log("Inner End"); } IEnumerator OuterCoroutine() { Debug.Log("Outer Start"); yield return StartCoroutine(InnerCoroutine()); Debug.Log("Outer End"); } Coroutine runningCoroutine; void Start() { runningCoroutine = StartCoroutine(OuterCoroutine()); StopAllCoroutines(); } }
Attempts:
2 left
💡 Hint
StopAllCoroutines() stops the entire nested sequence immediately after the first yield.
✗ Incorrect
The first Debug.Log("Outer Start") runs, then InnerCoroutine starts and prints "Inner Start". But StopAllCoroutines() stops all coroutines before InnerCoroutine finishes or OuterCoroutine continues.