0
0
Unityframework~3 mins

Why WaitForSeconds and WaitForEndOfFrame in Unity? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could pause your game actions perfectly without freezing everything or guessing timing?

The Scenario

Imagine you want to make a game character pause for a moment before jumping or wait until the screen finishes drawing before updating something. Doing this by hand means you have to track time yourself or guess when the frame ends.

The Problem

Manually tracking time or frame completion is tricky and easy to mess up. You might freeze the whole game or cause glitches because you don't know exactly when to resume actions. It's slow and error-prone to write and debug.

The Solution

Using WaitForSeconds and WaitForEndOfFrame lets you pause actions smoothly without freezing the game. Unity handles the timing and frame updates for you, so your code stays clean and reliable.

Before vs After
Before
float timer = 0f;
while(timer < 2f) {
  timer += Time.deltaTime;
  // wait manually
}
// continue after 2 seconds
After
yield return new WaitForSeconds(2f);
// continue after 2 seconds
What It Enables

You can easily create smooth delays and frame-dependent actions that keep your game responsive and bug-free.

Real Life Example

In a game, you want to show a message for 3 seconds before hiding it, or update a camera after the frame finishes rendering to avoid flicker.

Key Takeaways

Manual timing is hard and error-prone.

WaitForSeconds and WaitForEndOfFrame handle timing and frame waits for you.

They make your game actions smooth and reliable.