These help you pause actions in your game for a short time or until the frame finishes. This makes your game feel smooth and timed right.
WaitForSeconds and WaitForEndOfFrame in Unity
yield return new WaitForSeconds(seconds); yield return new WaitForEndOfFrame();
Both are used inside a coroutine, which is a special function that can pause and resume.
WaitForSeconds pauses for a set time; WaitForEndOfFrame waits until the frame is fully drawn.
IEnumerator ExampleWaitSeconds() {
Debug.Log("Start waiting...");
yield return new WaitForSeconds(2f);
Debug.Log("2 seconds passed!");
}IEnumerator ExampleWaitEndOfFrame() {
Debug.Log("Before frame ends");
yield return new WaitForEndOfFrame();
Debug.Log("After frame ends");
}This script waits 3 seconds, prints a message, then waits until the frame ends and prints another message.
using UnityEngine; using System.Collections; public class WaitExample : MonoBehaviour { void Start() { StartCoroutine(WaitAndPrint()); } IEnumerator WaitAndPrint() { Debug.Log("Waiting for 3 seconds..."); yield return new WaitForSeconds(3f); Debug.Log("3 seconds passed!"); Debug.Log("Waiting for end of frame..."); yield return new WaitForEndOfFrame(); Debug.Log("Frame ended!"); } }
Coroutines must be started with StartCoroutine to work.
WaitForSeconds uses game time, so it pauses even if the frame rate changes.
WaitForEndOfFrame is useful for things like taking screenshots or updating UI after rendering.
Use WaitForSeconds to pause for a set time inside coroutines.
Use WaitForEndOfFrame to run code after the current frame finishes drawing.
Both help make timed and smooth actions in your Unity game.
Practice
WaitForSeconds do in a Unity coroutine?Solution
Step 1: Understand the purpose of WaitForSeconds
WaitForSeconds pauses the coroutine for a set number of seconds before continuing.Step 2: Compare with other options
WaitForEndOfFrame waits for the frame to finish, not a time delay. The others are incorrect behaviors.Final Answer:
Pauses the coroutine for a specified amount of time. -> Option AQuick Check:
WaitForSeconds = pause time [OK]
- Confusing WaitForSeconds with WaitForEndOfFrame
- Thinking it stops coroutine permanently
- Assuming it resumes immediately
Solution
Step 1: Recall correct syntax for WaitForEndOfFrame
It requires creating a new instance:new WaitForEndOfFrame().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'.Final Answer:
yield return new WaitForEndOfFrame(); -> Option BQuick Check:
Correct syntax = yield return new WaitForEndOfFrame() [OK]
- Omitting 'new' keyword
- Forgetting 'yield return'
- Using WaitForEndOfFrame like a method without 'new'
IEnumerator Example() {
Debug.Log("Start");
yield return new WaitForEndOfFrame();
Debug.Log("End of Frame");
yield return new WaitForSeconds(1f);
Debug.Log("After 1 second");
}Solution
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".Step 2: Confirm output sequence
The logs appear in order: Start, End of Frame, After 1 second.Final Answer:
"Start" -> "End of Frame" -> "After 1 second" -> Option CQuick Check:
WaitForEndOfFrame runs before WaitForSeconds delay [OK]
- Assuming WaitForSeconds runs before WaitForEndOfFrame
- Mixing log order
- Ignoring yield return pauses
IEnumerator WaitExample() {
Debug.Log("Before wait");
yield WaitForSeconds(2f);
Debug.Log("After wait");
}Solution
Step 1: Check syntax for WaitForSeconds usage
WaitForSeconds must be instantiated with 'new', so it should be 'yield return new WaitForSeconds(2f);'.Step 2: Verify other statements
Coroutines can use yield statements and Debug.Log is allowed anywhere.Final Answer:
Missing 'new' keyword before WaitForSeconds. -> Option AQuick Check:
Always use 'yield return new WaitForSeconds()' [OK]
- Omitting 'new' keyword
- Using 'yield' without 'return'
- Thinking Debug.Log is invalid in coroutines
Solution
Step 1: Understand the timing requirements
The UI should update right after the frame ends, so first wait for WaitForEndOfFrame, then update UI.Step 2: Wait 0.5 seconds before next update
After the first update, wait 0.5 seconds using WaitForSeconds, then update UI again.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.Final Answer:
yield return new WaitForEndOfFrame(); UpdateUI(); yield return new WaitForSeconds(0.5f); UpdateUI(); -> Option DQuick Check:
EndOfFrame first, then WaitForSeconds delay [OK]
- Updating UI before WaitForEndOfFrame
- Mixing order of waits and updates
- Calling UpdateUI twice without wait
Ready to prove it?
Pass the test and this pattern moves unknown
