CodeBlanks Logo Icon CodeBlanks

WaitForSeconds

Learn to pause a coroutine with WaitForSeconds.

Time delay with WaitForSeconds

When a coroutine must wait for a designated time in seconds rather than frames, WaitForSeconds is utilized. Writing yield return new WaitForSeconds(2.0f); pauses execution for precisely 2 seconds.

IEnumerator ReloadWeapon() {
    Debug.Log("Reloading weapon...");
    yield return new WaitForSeconds(2.5f);
    Debug.Log("Weapon reloaded!");
}

Weapon reloads, ability cooldowns, and on-screen dialogue durations are managed through this technique.

Time.timeScale and WaitForSecondsRealtime

WaitForSeconds is scaled by the game's internal Time.timeScale; when the game is paused (timeScale = 0), countdowns freeze as well.

// Realtime delay that runs even when the game is paused
yield return new WaitForSecondsRealtime(1.0f);

For pause menu UI transitions or real-world countdown timers, WaitForSecondsRealtime is preferred.

Practice Print delay duration info

Assign "2.5" to delay and "Seconds" to unit. Print Delay: 2.5 | Unit: Seconds to the console.

Starter code

Player.cs
using System;

class Program
{
    static void Main()
    {
        // delay ve unit değerlerini tanımlayıp yazdır
    }
}

Sample solution

Player.cs
using System;

class Program
{
    static void Main()
    {
        string delay = "2.5";
        string unit = "Seconds";
        Console.WriteLine("Delay: " + delay + " | Unit: " + unit);
    }
}

Expected output

Delay: 2.5 | Unit: Seconds

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which statement is used to pause a coroutine for 3 seconds based on game time in Unity?

  • System.Threading.Thread.Sleep(3000);
  • yield return new WaitForSeconds(3f); Correct answer
  • yield return 3;
  • wait.Seconds(3);

Explanation: yield return new WaitForSeconds(3f) is the object providing a 3-second delay based on in-game time.

2 Instantiate second-based delay object

Question
yield return new ____(1.5f);
Answer
yield return new WaitForSeconds(1.5f);

Explanation: The WaitForSeconds class is instantiated to perform second-based delays.

3 Time delay terms

Term Meaning
WaitForSeconds Wait seconds in game time
WaitForSecondsRealtime Wait seconds by real clock
Time.timeScale = 0 Freeze in-game time
Delay Gap between operations

All lessons in Intermediate