Bird
Raised Fist0
Unityframework~20 mins

Coroutine basics (IEnumerator) in Unity - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a Coroutine in Unity?
easy
A. To handle user input events only
B. To create new game objects instantly
C. To pause and resume code execution over time without freezing the game
D. To permanently stop the game

Solution

  1. Step 1: Understand Coroutine behavior

    Coroutines allow code to pause and continue later, useful for timed actions.
  2. Step 2: Compare options

    Only To pause and resume code execution over time without freezing the game describes pausing and resuming code smoothly without freezing the game.
  3. Final Answer:

    To pause and resume code execution over time without freezing the game -> Option C
  4. Quick Check:

    Coroutine = pause and resume code [OK]
Hint: Coroutines pause code without freezing game [OK]
Common Mistakes:
  • Thinking coroutines create objects
  • Confusing coroutines with event handlers
  • Believing coroutines stop the game
2. Which of the following is the correct way to declare a coroutine method in Unity?
easy
A. public IEnumerator MyCoroutine() { yield return null; }
B. public string MyCoroutine() { yield return null; }
C. public int MyCoroutine() { yield return null; }
D. public void MyCoroutine() { yield return null; }

Solution

  1. Step 1: Recall coroutine method signature

    Coroutine methods must return IEnumerator to support yield return.
  2. Step 2: Check each option's return type

    Only public IEnumerator MyCoroutine() { yield return null; } returns IEnumerator and uses yield return correctly.
  3. Final Answer:

    public IEnumerator MyCoroutine() { yield return null; } -> Option A
  4. Quick Check:

    Coroutine method returns IEnumerator [OK]
Hint: Coroutine methods always return IEnumerator [OK]
Common Mistakes:
  • Using void as return type
  • Returning int or string instead of IEnumerator
  • Forgetting yield return inside coroutine
3. What will be the output of this Unity coroutine code snippet?
IEnumerator PrintNumbers() {
    for (int i = 1; i <= 3; i++) {
        Debug.Log(i);
        yield return new WaitForSeconds(1);
    }
}

StartCoroutine(PrintNumbers());
medium
A. Prints 1, 2, 3 immediately without delay
B. Prints 1, then waits 1 second, prints 2, waits 1 second, prints 3
C. Prints 3 three times with 1 second delay
D. No output because coroutine is not started properly

Solution

  1. Step 1: Understand the for loop with yield

    The loop prints i, then waits 1 second before next iteration.
  2. Step 2: Trace output timing

    It prints 1, waits 1 second, prints 2, waits 1 second, then prints 3.
  3. Final Answer:

    Prints 1, then waits 1 second, prints 2, waits 1 second, prints 3 -> Option B
  4. Quick Check:

    yield return WaitForSeconds delays output [OK]
Hint: yield return pauses loop iteration [OK]
Common Mistakes:
  • Assuming all prints happen instantly
  • Thinking it prints 3 repeatedly
  • Forgetting to start coroutine with StartCoroutine
4. Identify the error in this coroutine code:
IEnumerator Example() {
    Debug.Log("Start");
    yield WaitForSeconds(2);
    Debug.Log("End");
}

StartCoroutine(Example());
medium
A. Missing 'return' keyword before WaitForSeconds
B. Coroutine method should return void
C. StartCoroutine cannot be used with IEnumerator
D. Debug.Log cannot be used inside coroutines

Solution

  1. Step 1: Check yield statement syntax

    Correct syntax requires 'yield return' before WaitForSeconds.
  2. Step 2: Verify other parts

    Coroutine returns IEnumerator, StartCoroutine usage is correct, Debug.Log works inside coroutines.
  3. Final Answer:

    Missing 'return' keyword before WaitForSeconds -> Option A
  4. Quick Check:

    Use 'yield return' with WaitForSeconds [OK]
Hint: Always write 'yield return' before waiting [OK]
Common Mistakes:
  • Writing 'yield' without 'return'
  • Using void return type for coroutine
  • Thinking StartCoroutine can't start IEnumerator
5. You want to create a coroutine that fades out a UI element over 3 seconds smoothly. Which approach correctly uses IEnumerator and yield return to achieve this?
hard
A. Change opacity once, then yield return new WaitForSeconds(3) without gradual change
B. Call StartCoroutine inside Update() every frame without stopping
C. Use a for loop without yield return to change opacity instantly
D. Use a loop that changes opacity gradually, yielding null each frame until 3 seconds pass

Solution

  1. Step 1: Understand smooth fading with coroutine

    Smooth fade requires gradual opacity change over time with frame-by-frame updates.
  2. Step 2: Analyze options for gradual change and yielding

    Use a loop that changes opacity gradually, yielding null each frame until 3 seconds pass uses a loop with yield return null to wait one frame each iteration, allowing smooth fade over 3 seconds.
  3. Final Answer:

    Use a loop that changes opacity gradually, yielding null each frame until 3 seconds pass -> Option D
  4. Quick Check:

    Gradual change + yield return null = smooth fade [OK]
Hint: Yield null each frame for smooth animation [OK]
Common Mistakes:
  • Waiting full time without gradual change
  • Not yielding inside loop causing freeze
  • Starting coroutine repeatedly without stop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard