DEV Community

Cover image for Countdown Timer in Unity: How I Created a Timer for My Game
Alok Krishali
Alok Krishali

Posted on

Countdown Timer in Unity: How I Created a Timer for My Game

Have you ever played a game where you have only a few seconds to complete a level? That simple countdown can make the gameplay much more exciting.

While working on my Unity game, I wanted to add a countdown timer that would give the player a limited amount of time to complete a level. Instead of using a complicated system, I decided to create a simple timer using C# and Unity UI.

In this tutorial, I'll show you how I created a countdown timer in Unity step by step. We will create the timer, display it on the screen, decrease the time every second, and stop the timer when it reaches zero.

By the end of this tutorial, you will have a working timer in Unity that you can easily use in your own game.

What We Are Going to Create

The idea is simple.

When the level starts, the timer will begin counting down.

For example:

60 → 59 → 58 → 57 → ... → 1 → 0

When the timer reaches zero, we can perform an action such as:

  • End the level
  • Show a game-over screen
  • Reduce the player's score
  • Restart the level
  • Complete a mission
  • Trigger another gameplay event

For this tutorial, I'll keep the basic system simple so you can easily modify it for your own game.

Step 1: Create a New Timer Script

The first thing I did was create a C# script for the timer.

In your Unity project, create a new C# script and name it:

CountdownTimer

Open the script and start with the following code:

using UnityEngine;

public class CountdownTimer : MonoBehaviour
{
    public float timeRemaining = 60f;

    void Update()
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
        }
        else
        {
            timeRemaining = 0;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the basic part of our Unity countdown timer.

Let's understand what is happening here.

timeRemaining

public float timeRemaining = 60f;
Enter fullscreen mode Exit fullscreen mode

This variable stores the amount of time remaining.

I started the timer at 60 seconds, but you can change it to any value you want.

For example:

public float timeRemaining = 30f;
Enter fullscreen mode Exit fullscreen mode

This will create a 30-second timer.

Time.deltaTime

The important part is:

timeRemaining -= Time.deltaTime;
Enter fullscreen mode Exit fullscreen mode

Time.deltaTime gives us the time that has passed since the previous frame.

Instead of manually decreasing the timer every second, Unity continuously reduces the remaining time based on the actual game time.

This makes the timer work correctly even when the game's frame rate changes.

Step 2: Add the Timer to the Game

Now we need to add our timer script to a GameObject.

In the Unity Hierarchy:

  1. Right-click in the Hierarchy.
  2. Select Create Empty.
  3. Rename it to GameManager.
  4. Select the GameManager.
  5. Drag the CountdownTimer script onto it.

Now the timer script is connected to our game.

If you press Play, the timeRemaining value will start decreasing.

However, there is one problem.

The player cannot see the timer yet.

So let's add a UI element.

Step 3: Create the Timer UI

For my game, I wanted the remaining time to appear at the top of the screen.

First, create a Canvas if you don't already have one.

Then:

  1. Right-click the Canvas.
  2. Select UI → Text - TextMeshPro.
  3. Rename it to TimerText.
  4. Position it where you want the timer to appear.
  5. Set the initial text to something like 60.

I recommend using TextMeshPro because it gives you better control over fonts, size, alignment, and visual appearance.

Your Hierarchy might look something like this:

Canvas
    └── TimerText

GameManager
    └── CountdownTimer
Enter fullscreen mode Exit fullscreen mode

Now we need to connect this UI text to our C# script.

Step 4: Connect the Timer UI With C

Let's modify our script.

using UnityEngine;
using TMPro;

public class CountdownTimer : MonoBehaviour
{
    public float timeRemaining = 60f;
    public TextMeshProUGUI timerText;

    void Update()
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
        }
        else
        {
            timeRemaining = 0;
        }

        timerText.text = Mathf.Ceil(timeRemaining).ToString();
    }
}
Enter fullscreen mode Exit fullscreen mode

There are two important things here.

First, we added:

using TMPro;
Enter fullscreen mode Exit fullscreen mode

This allows us to work with TextMeshPro.

Then we created:

public TextMeshProUGUI timerText;
Enter fullscreen mode Exit fullscreen mode

This variable will hold a reference to the UI text that displays our countdown.

Step 5: Assign the Timer Text in Unity

Now go back to Unity.

Select your GameManager GameObject.

You should see the CountdownTimer component in the Inspector.

There will be a field called:

Timer Text

Simply drag your TimerText GameObject from the Hierarchy into this field.

Now Unity knows which UI element should display the countdown.

Press Play.

You should see something similar to:

60
59
58
57
56
...
Enter fullscreen mode Exit fullscreen mode

Congratulations!

You have created a basic countdown timer in Unity.

Step 6: Why I Used Mathf.Ceil

You may notice that the actual timer uses a float value.

For example:

59.83
58.72
57.51
Enter fullscreen mode Exit fullscreen mode

We don't want to show those decimal values to the player.

That's why I used:

Mathf.Ceil(timeRemaining)
Enter fullscreen mode Exit fullscreen mode

This converts the remaining time into a clean whole number.

So instead of displaying:

59.83
Enter fullscreen mode Exit fullscreen mode

the player sees:

60
Enter fullscreen mode Exit fullscreen mode

This makes the timer much easier to understand.

Step 7: Stop the Timer at Zero

We don't want the timer to continue into negative numbers.

That's why we use:

if (timeRemaining > 0)
{
    timeRemaining -= Time.deltaTime;
}
else
{
    timeRemaining = 0;
}
Enter fullscreen mode Exit fullscreen mode

Once the timer reaches zero, we simply set it to zero.

Now the countdown stops at:

0
Enter fullscreen mode Exit fullscreen mode

instead of continuing:

-1
-2
-3
Enter fullscreen mode Exit fullscreen mode

Step 8: What Happens When the Timer Reaches Zero?

This is where you can customize the timer for your own game.

For example, in my game, I could trigger a game-over function when the timer reaches zero.

We can modify the code like this:

using UnityEngine;
using TMPro;

public class CountdownTimer : MonoBehaviour
{
    public float timeRemaining = 60f;
    public TextMeshProUGUI timerText;

    private bool timerFinished = false;

    void Update()
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
        }
        else
        {
            timeRemaining = 0;

            if (!timerFinished)
            {
                timerFinished = true;
                TimerFinished();
            }
        }

        timerText.text = Mathf.Ceil(timeRemaining).ToString();
    }

    void TimerFinished()
    {
        Debug.Log("Time's Up!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Now TimerFinished() is called when the countdown reaches zero.

You could replace:

Debug.Log("Time's Up!");
Enter fullscreen mode Exit fullscreen mode

with your actual game logic.

For example:

GameOver();
Enter fullscreen mode Exit fullscreen mode

or:

CompleteMission();
Enter fullscreen mode Exit fullscreen mode

or:

RestartLevel();
Enter fullscreen mode Exit fullscreen mode

This makes the timer useful for actual gameplay rather than just displaying numbers.

Step 9: Add Minutes and Seconds

If your game has a longer timer, displaying only seconds may not look very good.

For example:

125
124
123
Enter fullscreen mode Exit fullscreen mode

Instead, you may want:

02:05
02:04
02:03
Enter fullscreen mode Exit fullscreen mode

We can easily change the display.

Replace:

timerText.text = Mathf.Ceil(timeRemaining).ToString();
Enter fullscreen mode Exit fullscreen mode

with:

int minutes = Mathf.FloorToInt(timeRemaining / 60);
int seconds = Mathf.FloorToInt(timeRemaining % 60);

timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
Enter fullscreen mode Exit fullscreen mode

Now a 125-second timer will display:

02:05
Enter fullscreen mode Exit fullscreen mode

and then:

02:04
02:03
02:02
Enter fullscreen mode Exit fullscreen mode

This is a much better format for many games.

Step 10: Add a Visual Warning

A countdown timer becomes even more useful when it gives the player a warning that time is running out.

For example, when there are only 10 seconds remaining, we can change the timer's appearance.

A simple approach is:

if (timeRemaining <= 10)
{
    timerText.color = Color.red;
}
Enter fullscreen mode Exit fullscreen mode

Now the timer can become red when the player is running out of time.

You can take this further by adding:

  • Timer animation
  • Sound effects
  • Screen effects
  • Countdown sounds
  • Camera shake
  • Warning messages

For example:

TIME: 10
TIME: 9
TIME: 8
Enter fullscreen mode Exit fullscreen mode

The final few seconds can create a lot more tension during gameplay.

Complete Countdown Timer Script

After putting everything together, this is the basic version I would use for a simple game timer:

using UnityEngine;
using TMPro;

public class CountdownTimer : MonoBehaviour
{
    public float timeRemaining = 60f;
    public TextMeshProUGUI timerText;

    private bool timerFinished = false;

    void Update()
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
        }
        else
        {
            timeRemaining = 0;

            if (!timerFinished)
            {
                timerFinished = true;
                TimerFinished();
            }
        }

        int minutes = Mathf.FloorToInt(timeRemaining / 60);
        int seconds = Mathf.FloorToInt(timeRemaining % 60);

        timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

        if (timeRemaining <= 10)
        {
            timerText.color = Color.red;
        }
    }

    void TimerFinished()
    {
        Debug.Log("Time's Up!");
    }
}
Enter fullscreen mode Exit fullscreen mode

You can now connect this script to your UI and use it as the foundation for a timer system in your Unity game.

How I Used the Timer in My Game

The main reason I created this system was to add a little more pressure to the gameplay.

Without a timer, a player can sometimes take as much time as they want to complete a level. That's fine for some games, but for challenge-based gameplay, a countdown can make each level feel more engaging.

The basic gameplay flow becomes:

Start Level
     ↓
Start Countdown
     ↓
Player Plays
     ↓
Timer Decreases
     ↓
Player Completes Level?
     ↓
Yes → Level Complete
     ↓
No → Timer Reaches 0
     ↓
Time's Up / Game Over
Enter fullscreen mode Exit fullscreen mode

This simple system can be adapted to many different types of games.

Where You Can Use a Timer in Unity

A timer in Unity can be used in many gameplay situations.

For example:

Puzzle Games

Give the player 60 seconds to solve a puzzle.

Action Games

Give the player a limited amount of time to defeat enemies.

Racing Games

Use a countdown to control race events or time trials.

Survival Games

Challenge the player to survive for a specific amount of time.

Platform Games

Give the player a limited amount of time to reach the finish point.

Mission-Based Games

Create missions where the player needs to complete an objective before the timer reaches zero.

Common Problems When Creating a Timer in Unity

While creating a countdown timer is relatively simple, there are a few common problems you may encounter.

Timer Goes Below Zero

Make sure you set:

timeRemaining = 0;
Enter fullscreen mode Exit fullscreen mode

when the countdown finishes.

Timer Shows Decimal Numbers

If you see:

59.82342
Enter fullscreen mode Exit fullscreen mode

use:

Mathf.Ceil(timeRemaining)
Enter fullscreen mode Exit fullscreen mode

or format the timer as minutes and seconds.

Timer UI Is Not Updating

Check that you have dragged the TimerText object into the Timer Text field in the Inspector.

Timer Finishes Multiple Times

Use a boolean such as:

private bool timerFinished = false;
Enter fullscreen mode Exit fullscreen mode

This prevents your end-of-timer function from being called repeatedly.

Final Thoughts

Creating a countdown timer in Unity doesn't have to be complicated. I started with a simple float variable, reduced it using Time.deltaTime, and then connected it to a TextMeshPro UI element.

From there, it is easy to expand the system with game-over logic, sounds, animations, warnings, and other gameplay features.

The nice thing about this approach is that you don't need a complicated timer framework. You can start with this simple system and build on it as your game becomes more advanced.

If you're learning Unity or working on your own game, I recommend starting with the simple version first. Once it works, add features one at a time.

That's how I created a simple countdown timer for my Unity game, and you can use the same approach in your own project.

Frequently Asked Questions

1. How do I create a countdown timer in Unity?

You can create a countdown timer in Unity using a float variable and decrease it with Time.deltaTime. You can then display the remaining time using a TextMeshPro UI element.

2. How do I create a timer in Unity using C#?

Create a C# script with a float variable for the time and decrease it inside the Update() method using Time.deltaTime.

3. How do I display a countdown timer in Unity?

Create a TextMeshPro UI text object and connect it to your timer script. Update the text using the remaining time.

4. How do I stop a timer when it reaches zero in Unity?

Check whether the remaining time is greater than zero. If it reaches zero, set the value to zero and run your desired function.

5. Can I use a countdown timer for a Unity game level?

Yes. A countdown timer can be used for timed levels, missions, puzzles, races, survival challenges, and many other gameplay situations.

Top comments (0)