0
0
Unityframework~20 mins

Stopping coroutines in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
    }
}
ANo output
B
Start
End
CEnd
DStart
Attempts:
2 left
💡 Hint
Stopping a coroutine by name immediately stops it before it can continue.
Predict Output
intermediate
2: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);
    }
}
ARunning
B
Running
Finished
CFinished
DNo output
Attempts:
2 left
💡 Hint
Stopping a coroutine by IEnumerator reference stops it immediately after the first yield.
🔧 Debug
advanced
2: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());
    }
}
AStopCoroutine must be called before StartCoroutine.
BStopCoroutine is called with a new IEnumerator instance, not the running one.
CStopCoroutine cannot stop infinite loops.
DThe coroutine never yields, so it can't be stopped.
Attempts:
2 left
💡 Hint
Check how the coroutine reference is passed to StopCoroutine.
🧠 Conceptual
advanced
1:30remaining
What happens when you call StopAllCoroutines()?
In Unity, what is the effect of calling StopAllCoroutines() inside a MonoBehaviour?
AIt stops all coroutines running on all GameObjects in the scene.
BIt pauses all coroutines but does not stop them.
CIt stops all coroutines running on the current MonoBehaviour instance.
DIt stops only the last started coroutine on the current MonoBehaviour.
Attempts:
2 left
💡 Hint
Think about the scope of coroutines in Unity.
Predict Output
expert
3: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();
    }
}
A
Outer Start
Inner Start
B
Outer Start
Inner Start
Inner End
Outer End
COuter Start
DNo output
Attempts:
2 left
💡 Hint
StopAllCoroutines() stops the entire nested sequence immediately after the first yield.