What will be the output of this Unity C# coroutine code snippet?
IEnumerator ExampleCoroutine() {
Debug.Log("Start");
yield return new WaitForSeconds(1f);
Debug.Log("After 1 second");
}Think about how WaitForSeconds pauses the coroutine.
The coroutine prints 'Start' immediately, then pauses for 1 second due to WaitForSeconds(1f), and finally prints 'After 1 second'.
Consider this coroutine snippet. When will the message 'End of frame reached' be printed?
IEnumerator FrameCoroutine() {
Debug.Log("Before waiting");
yield return new WaitForEndOfFrame();
Debug.Log("End of frame reached");
}Think about the frame lifecycle in Unity and when WaitForEndOfFrame resumes.
WaitForEndOfFrame waits until the frame finishes rendering, then resumes the coroutine, so the message prints after rendering.
Look at this coroutine code. Why does it never print 'Second message'?
IEnumerator TestCoroutine() {
Debug.Log("First message");
yield return new WaitForSeconds(-1f);
Debug.Log("Second message");
}Check how WaitForSeconds handles negative values.
Using a negative time in WaitForSeconds causes the coroutine to wait indefinitely and never resume, so the second message never prints.
Which statement correctly describes the difference between WaitForSeconds and WaitForEndOfFrame in Unity coroutines?
Think about timing: fixed seconds vs frame lifecycle.
WaitForSeconds delays the coroutine by a specified number of seconds. WaitForEndOfFrame waits until the frame rendering completes before continuing.
Given this coroutine, how many times will 'Frame ended' print after starting it once?
IEnumerator LoopCoroutine() {
while (true) {
yield return new WaitForEndOfFrame();
Debug.Log("Frame ended");
}
}Consider what an infinite loop with WaitForEndOfFrame means in a coroutine.
The coroutine waits until the end of each frame, then prints the message, and repeats forever, printing once per frame.