Coroutines let your program wait or pause without stopping everything. They help do tasks step-by-step over time.
Coroutine basics (IEnumerator) in Unity
IEnumerator CoroutineName() {
// code before waiting
yield return new WaitForSeconds(seconds);
// code after waiting
}IEnumerator is a special return type that allows pausing and resuming the function.
yield return tells Unity when to pause and when to continue the coroutine.
IEnumerator WaitAndPrint() {
yield return new WaitForSeconds(2);
Debug.Log("Hello after 2 seconds");
}IEnumerator RepeatMessage() {
while(true) {
Debug.Log("Repeating every 1 second");
yield return new WaitForSeconds(1);
}
}This program starts a coroutine when the game begins. It prints a message, waits 3 seconds, then prints another message.
using UnityEngine; using System.Collections; public class CoroutineExample : MonoBehaviour { void Start() { StartCoroutine(WaitAndPrint()); } IEnumerator WaitAndPrint() { Debug.Log("Start waiting..."); yield return new WaitForSeconds(3); Debug.Log("3 seconds passed!"); } }
Coroutines run alongside other code without freezing the game.
You must start coroutines using StartCoroutine() in Unity.
Coroutines can pause on many things, not just time (like waiting for a condition).
Coroutines let you pause and resume code smoothly over time.
Use IEnumerator and yield return to create coroutines.
Start coroutines with StartCoroutine() to run them in Unity.
Practice
Coroutine in Unity?Solution
Step 1: Understand Coroutine behavior
Coroutines allow code to pause and continue later, useful for timed actions.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.Final Answer:
To pause and resume code execution over time without freezing the game -> Option CQuick Check:
Coroutine = pause and resume code [OK]
- Thinking coroutines create objects
- Confusing coroutines with event handlers
- Believing coroutines stop the game
Solution
Step 1: Recall coroutine method signature
Coroutine methods must return IEnumerator to support yield return.Step 2: Check each option's return type
Only public IEnumerator MyCoroutine() { yield return null; } returns IEnumerator and uses yield return correctly.Final Answer:
public IEnumerator MyCoroutine() { yield return null; } -> Option AQuick Check:
Coroutine method returns IEnumerator [OK]
- Using void as return type
- Returning int or string instead of IEnumerator
- Forgetting yield return inside coroutine
IEnumerator PrintNumbers() {
for (int i = 1; i <= 3; i++) {
Debug.Log(i);
yield return new WaitForSeconds(1);
}
}
StartCoroutine(PrintNumbers());Solution
Step 1: Understand the for loop with yield
The loop prints i, then waits 1 second before next iteration.Step 2: Trace output timing
It prints 1, waits 1 second, prints 2, waits 1 second, then prints 3.Final Answer:
Prints 1, then waits 1 second, prints 2, waits 1 second, prints 3 -> Option BQuick Check:
yield return WaitForSeconds delays output [OK]
- Assuming all prints happen instantly
- Thinking it prints 3 repeatedly
- Forgetting to start coroutine with StartCoroutine
IEnumerator Example() {
Debug.Log("Start");
yield WaitForSeconds(2);
Debug.Log("End");
}
StartCoroutine(Example());Solution
Step 1: Check yield statement syntax
Correct syntax requires 'yield return' before WaitForSeconds.Step 2: Verify other parts
Coroutine returns IEnumerator, StartCoroutine usage is correct, Debug.Log works inside coroutines.Final Answer:
Missing 'return' keyword before WaitForSeconds -> Option AQuick Check:
Use 'yield return' with WaitForSeconds [OK]
- Writing 'yield' without 'return'
- Using void return type for coroutine
- Thinking StartCoroutine can't start IEnumerator
IEnumerator and yield return to achieve this?Solution
Step 1: Understand smooth fading with coroutine
Smooth fade requires gradual opacity change over time with frame-by-frame updates.Step 2: Analyze options for gradual change and yielding
Use a loop that changes opacity gradually, yieldingnulleach frame until 3 seconds pass uses a loop with yield return null to wait one frame each iteration, allowing smooth fade over 3 seconds.Final Answer:
Use a loop that changes opacity gradually, yielding null each frame until 3 seconds pass -> Option DQuick Check:
Gradual change + yield return null = smooth fade [OK]
- Waiting full time without gradual change
- Not yielding inside loop causing freeze
- Starting coroutine repeatedly without stop
Ready to prove it?
Pass the test and this pattern moves unknown
