Challenge - 5 Problems
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of chained coroutines with WaitForSeconds
What will be the output sequence and timing when running this Unity coroutine chaining code?
Unity
using System.Collections; using UnityEngine; public class TestChain : MonoBehaviour { void Start() { StartCoroutine(First()); } IEnumerator First() { Debug.Log("Start First"); yield return StartCoroutine(Second()); Debug.Log("End First"); } IEnumerator Second() { Debug.Log("Start Second"); yield return new WaitForSeconds(1); Debug.Log("End Second"); } }
Attempts:
2 left
💡 Hint
Remember that yield return StartCoroutine waits for the inner coroutine to finish before continuing.
✗ Incorrect
The First coroutine starts and logs 'Start First'. It then yields return StartCoroutine(Second()), so it waits until Second finishes. Second logs 'Start Second', waits 1 second, then logs 'End Second'. After Second finishes, First resumes and logs 'End First'.
🧠 Conceptual
intermediate1:30remaining
Understanding yield return null in coroutine chaining
In Unity coroutine chaining, what does yield return null do inside a coroutine?
Attempts:
2 left
💡 Hint
Think about what happens when you want to wait just one frame.
✗ Incorrect
yield return null pauses the coroutine and resumes it in the next frame, allowing other code to run in between.
🔧 Debug
advanced2:00remaining
Identify the error in coroutine chaining code
What error will this Unity coroutine chaining code produce when run?
Unity
IEnumerator First()
{
Debug.Log("First start");
yield return Second();
Debug.Log("First end");
}
IEnumerator Second()
{
Debug.Log("Second start");
yield return new WaitForSeconds(1);
Debug.Log("Second end");
}Attempts:
2 left
💡 Hint
Check how coroutines are started and chained properly.
✗ Incorrect
yield return Second() returns the IEnumerator but does not start it as a coroutine. So First does not wait for Second to finish. To wait properly, use yield return StartCoroutine(Second()).
📝 Syntax
advanced2:00remaining
Correct syntax for chaining multiple coroutines
Which option correctly chains two coroutines so that Second runs after First finishes?
Unity
IEnumerator First() { Debug.Log("First"); yield return new WaitForSeconds(1); }
IEnumerator Second() { Debug.Log("Second"); yield return new WaitForSeconds(1); }Attempts:
2 left
💡 Hint
Remember how to wait for one coroutine to finish before starting another.
✗ Incorrect
Using yield return StartCoroutine(...) waits for the coroutine to finish before continuing. Option B just returns the IEnumerator without starting it properly. Option B starts both coroutines simultaneously. Option B just waits fixed times without chaining coroutines.
🚀 Application
expert3:00remaining
Chaining coroutines with conditional wait and result passing
Given these two coroutines, what will be the value of 'result' after running the Chain coroutine?
Unity
IEnumerator WaitAndReturn(int waitSeconds, System.Action<int> callback) { yield return new WaitForSeconds(waitSeconds); callback(waitSeconds * 2); } IEnumerator Chain() { int result = 0; yield return StartCoroutine(WaitAndReturn(2, x => result = x)); Debug.Log($"Result is {result}"); }
Attempts:
2 left
💡 Hint
Look at how the callback sets the result after waiting.
✗ Incorrect
WaitAndReturn waits 2 seconds, then calls callback with 2*2=4. Chain waits for WaitAndReturn to finish, so result is set to 4 before logging.