Bird
Raised Fist0
Unityframework~10 mins

WaitForSeconds and WaitForEndOfFrame in Unity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - WaitForSeconds and WaitForEndOfFrame
Start Coroutine
↓
Yield WaitForSeconds
↓
Wait real time seconds
↓
Resume after delay
↓
Yield WaitForEndOfFrame
↓
Wait until frame ends
↓
Resume next frame
↓
Coroutine ends
The coroutine pauses at WaitForSeconds for a set time, then resumes and pauses again at WaitForEndOfFrame until the current frame finishes, then continues.
Execution Sample
Unity
IEnumerator Example() {
  Debug.Log("Start");
  yield return new WaitForSeconds(2);
  Debug.Log("After 2 seconds");
  yield return new WaitForEndOfFrame();
  Debug.Log("End of frame");
}
This coroutine logs a message, waits 2 seconds, logs again, waits until the frame ends, then logs a final message.
Execution Table
StepActionYieldWait Time / ConditionOutput
1Start coroutineNoneNoneLogs: Start
2Yield WaitForSecondsWaitForSeconds2 seconds real timePauses coroutine
3Resume after 2 secondsNoneTime passedLogs: After 2 seconds
4Yield WaitForEndOfFrameWaitForEndOfFrameUntil current frame endsPauses coroutine
5Resume after frame endsNoneFrame finishedLogs: End of frame
6Coroutine endsNoneNoneCoroutine stops
💡 Coroutine stops after completing all yield instructions
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Coroutine StateNot startedWaiting 2 secondsWaiting end of frameCompleted
Key Moments - 3 Insights
Why does the coroutine pause at WaitForSeconds?
Because WaitForSeconds tells Unity to pause the coroutine for the specified real time seconds before continuing, as shown in step 2 of the execution_table.
What does WaitForEndOfFrame wait for exactly?
It waits until the current frame finishes rendering before resuming the coroutine, as shown in step 4 and 5 of the execution_table.
Does WaitForSeconds wait for game frames or real time?
WaitForSeconds waits for real time seconds, not frames, so it can pause across multiple frames, as seen between steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the coroutine state after step 2?
AWaiting end of frame
BWaiting 2 seconds
CCompleted
DNot started
💡 Hint
Check variable_tracker row 'Coroutine State' after step 2
At which step does the coroutine resume after waiting for the frame to end?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
See execution_table step 5 where it logs 'End of frame'
If WaitForSeconds was changed to 0, what would happen at step 2?
ACoroutine pauses for 2 seconds
BCoroutine pauses until frame ends
CCoroutine continues immediately without pause
DCoroutine stops
💡 Hint
WaitForSeconds with 0 means no delay, so coroutine continues immediately after step 2
Concept Snapshot
WaitForSeconds pauses coroutine for given seconds (real time).
WaitForEndOfFrame pauses coroutine until current frame finishes rendering.
Use yield return with these to delay actions.
WaitForSeconds can span multiple frames.
WaitForEndOfFrame resumes next frame after rendering.
Both help control timing in Unity coroutines.
Full Transcript
This visual execution shows how Unity coroutines pause and resume using WaitForSeconds and WaitForEndOfFrame. The coroutine starts and logs a message. Then it yields WaitForSeconds, pausing for 2 seconds real time before resuming and logging again. Next, it yields WaitForEndOfFrame, pausing until the current frame finishes rendering. After the frame ends, it resumes and logs a final message before ending. Variables track the coroutine state changing from not started, to waiting 2 seconds, to waiting end of frame, then completed. Key moments clarify that WaitForSeconds waits real time seconds, not frames, and WaitForEndOfFrame waits until the frame finishes. The quiz tests understanding of coroutine states and timing. This helps beginners see exactly when and why coroutines pause and resume in Unity.

Practice

(1/5)
1. What does WaitForSeconds do in a Unity coroutine?
easy
A. Pauses the coroutine for a specified amount of time.
B. Waits until the end of the current frame before continuing.
C. Stops the coroutine permanently.
D. Immediately resumes the coroutine without delay.

Solution

  1. Step 1: Understand the purpose of WaitForSeconds

    WaitForSeconds pauses the coroutine for a set number of seconds before continuing.
  2. Step 2: Compare with other options

    WaitForEndOfFrame waits for the frame to finish, not a time delay. The others are incorrect behaviors.
  3. Final Answer:

    Pauses the coroutine for a specified amount of time. -> Option A
  4. Quick Check:

    WaitForSeconds = pause time [OK]
Hint: WaitForSeconds pauses by seconds, not frames or immediate resume [OK]
Common Mistakes:
  • Confusing WaitForSeconds with WaitForEndOfFrame
  • Thinking it stops coroutine permanently
  • Assuming it resumes immediately
2. Which of the following is the correct syntax to wait for the end of the current frame in a coroutine?
easy
A. yield return new WaitForSeconds(1f);
B. yield return new WaitForEndOfFrame();
C. yield return WaitForEndOfFrame();
D. yield WaitForEndOfFrame();

Solution

  1. Step 1: Recall correct syntax for WaitForEndOfFrame

    It requires creating a new instance: new WaitForEndOfFrame().
  2. Step 2: Check other options for syntax errors

    yield return new WaitForSeconds(1f); waits seconds, not end of frame. yield return WaitForEndOfFrame(); misses 'new'. yield WaitForEndOfFrame(); misses 'return' and 'new'.
  3. Final Answer:

    yield return new WaitForEndOfFrame(); -> Option B
  4. Quick Check:

    Correct syntax = yield return new WaitForEndOfFrame() [OK]
Hint: Always use 'yield return new' with WaitForEndOfFrame [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Forgetting 'yield return'
  • Using WaitForEndOfFrame like a method without 'new'
3. What will be the output order of the following Unity coroutine code?
IEnumerator Example() {
    Debug.Log("Start");
    yield return new WaitForEndOfFrame();
    Debug.Log("End of Frame");
    yield return new WaitForSeconds(1f);
    Debug.Log("After 1 second");
}
medium
A. "End of Frame" -> "Start" -> "After 1 second"
B. "Start" -> "After 1 second" -> "End of Frame"
C. "Start" -> "End of Frame" -> "After 1 second"
D. "After 1 second" -> "Start" -> "End of Frame"

Solution

  1. Step 1: Trace the coroutine execution order

    First, "Start" logs immediately. Then it waits until the end of the frame, logs "End of Frame". Then waits 1 second, logs "After 1 second".
  2. Step 2: Confirm output sequence

    The logs appear in order: Start, End of Frame, After 1 second.
  3. Final Answer:

    "Start" -> "End of Frame" -> "After 1 second" -> Option C
  4. Quick Check:

    WaitForEndOfFrame runs before WaitForSeconds delay [OK]
Hint: WaitForEndOfFrame logs before WaitForSeconds delay [OK]
Common Mistakes:
  • Assuming WaitForSeconds runs before WaitForEndOfFrame
  • Mixing log order
  • Ignoring yield return pauses
4. Identify the error in this coroutine code snippet:
IEnumerator WaitExample() {
    Debug.Log("Before wait");
    yield WaitForSeconds(2f);
    Debug.Log("After wait");
}
medium
A. Missing 'new' keyword before WaitForSeconds.
B. Coroutine cannot use yield statements.
C. WaitForSeconds cannot be used in coroutines.
D. Debug.Log statements are not allowed in coroutines.

Solution

  1. Step 1: Check syntax for WaitForSeconds usage

    WaitForSeconds must be instantiated with 'new', so it should be 'yield return new WaitForSeconds(2f);'.
  2. Step 2: Verify other statements

    Coroutines can use yield statements and Debug.Log is allowed anywhere.
  3. Final Answer:

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

    Always use 'yield return new WaitForSeconds()' [OK]
Hint: Remember 'yield return new' for WaitForSeconds [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using 'yield' without 'return'
  • Thinking Debug.Log is invalid in coroutines
5. You want to update a UI element exactly after the current frame finishes rendering, then wait 0.5 seconds before updating it again. Which coroutine sequence correctly achieves this?
hard
A. yield return new WaitForSeconds(0.5f); UpdateUI(); yield return new WaitForEndOfFrame(); UpdateUI();
B. UpdateUI(); yield return new WaitForSeconds(0.5f); yield return new WaitForEndOfFrame(); UpdateUI();
C. yield return new WaitForEndOfFrame(); yield return new WaitForSeconds(0.5f); UpdateUI(); UpdateUI();
D. yield return new WaitForEndOfFrame(); UpdateUI(); yield return new WaitForSeconds(0.5f); UpdateUI();

Solution

  1. Step 1: Understand the timing requirements

    The UI should update right after the frame ends, so first wait for WaitForEndOfFrame, then update UI.
  2. Step 2: Wait 0.5 seconds before next update

    After the first update, wait 0.5 seconds using WaitForSeconds, then update UI again.
  3. Step 3: Match the correct sequence

    yield return new WaitForEndOfFrame(); UpdateUI(); yield return new WaitForSeconds(0.5f); UpdateUI(); matches this order: WaitForEndOfFrame -> UpdateUI -> WaitForSeconds(0.5) -> UpdateUI.
  4. Final Answer:

    yield return new WaitForEndOfFrame(); UpdateUI(); yield return new WaitForSeconds(0.5f); UpdateUI(); -> Option D
  5. Quick Check:

    EndOfFrame first, then WaitForSeconds delay [OK]
Hint: Update after frame ends, then wait seconds before next update [OK]
Common Mistakes:
  • Updating UI before WaitForEndOfFrame
  • Mixing order of waits and updates
  • Calling UpdateUI twice without wait
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