0
0
Unityframework~20 mins

Coroutine basics (IEnumerator) 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 of this Coroutine?

Consider this Unity Coroutine code snippet. What will be printed to the console?

Unity
IEnumerator PrintNumbers()
{
    Debug.Log("Start");
    yield return new WaitForSeconds(1);
    Debug.Log("Middle");
    yield return new WaitForSeconds(1);
    Debug.Log("End");
}

// Assume this Coroutine is started once at time 0.
AOnly "Start" is printed, then Coroutine stops
B"Start", "Middle", and "End" all printed immediately
C"Start" immediately, then "Middle" after 1 second, then "End" after 2 seconds
D"Start" after 1 second, "Middle" after 2 seconds, "End" after 3 seconds
Attempts:
2 left
💡 Hint

Remember that yield return new WaitForSeconds(1) pauses the Coroutine for 1 second before continuing.

🧠 Conceptual
intermediate
1:30remaining
What happens if you call StopCoroutine on a running Coroutine?

In Unity, if you start a Coroutine and then call StopCoroutine on it before it finishes, what will happen?

AThe Coroutine immediately stops and no further code inside it runs
BThe Coroutine finishes running all remaining code instantly
CThe Coroutine pauses but resumes after some time automatically
DNothing happens; the Coroutine continues running normally
Attempts:
2 left
💡 Hint

Think about what stopping a Coroutine means in Unity.

🔧 Debug
advanced
2:00remaining
Why does this Coroutine never print "Done"?

Look at this Coroutine code. It never prints "Done". Why?

Unity
IEnumerator WaitAndPrint()
{
    yield return new WaitForSeconds(2);
    yield return null;
    Debug.Log("Done");
}

// Coroutine started once.
ABecause <code>yield return null</code> pauses only one frame, so "Done" should print
BBecause the Coroutine is never started properly
CBecause <code>yield return null</code> causes the Coroutine to restart endlessly
DBecause the Coroutine is stopped externally before reaching "Done"
Attempts:
2 left
💡 Hint

Think about what yield return null does in a Coroutine.

📝 Syntax
advanced
1:30remaining
Which option correctly defines a Coroutine method?

Which of these method signatures is valid for a Coroutine in Unity?

Apublic int MyCoroutine()
Bpublic void MyCoroutine()
Cpublic async Task MyCoroutine()
Dpublic IEnumerator MyCoroutine()
Attempts:
2 left
💡 Hint

Remember that Coroutines must return IEnumerator.

🚀 Application
expert
2:30remaining
How many times will "Tick" print in this Coroutine?

Consider this Coroutine that prints "Tick" every 0.5 seconds for 3 seconds total. How many times will "Tick" print?

Unity
IEnumerator TickCoroutine()
{
    float elapsed = 0f;
    while (elapsed < 3f)
    {
        Debug.Log("Tick");
        yield return new WaitForSeconds(0.5f);
        elapsed += 0.5f;
    }
}

// Coroutine started once.
A5 times
B6 times
C7 times
D3 times
Attempts:
2 left
💡 Hint

Count how many 0.5 second intervals fit into 3 seconds.