Facebook

How to make a countdown timer in Unity

by Liam Cubicle Published February 13, 2024

Making a countdown timer in Unity is very straightforward as it involves storing a float time value in a variable and subtracting the elapsed time of the last frame every frame to make it countdown. The timer can be displayed with a text object, displaying the float time value in minutes and seconds.

In this article, you will learn how to make a countdown timer in Unity with a step-by-step guide.

Creating a basic countdown timer in Unity

  • On your project window, create a new C# Script for the timer. Right-click in the Assets panel, then click on C# script under the Create menu. You can name it CountdownTimer.
  • Open the script, then create a float variable to set the total time for the countdown.
  • Then, using the Update method, subtract the elapsed time of the last frame every frame from the total time with Time.deltaTime.

Your script should look like this:

using UnityEngine;
using UnityEngine.UI;

public class CountdownTimer : MonoBehaviour
{
  //Set the total time for the countdown
  public float totalTime = 90;
  
  void Update()
  {
    if (totalTime > 0)
    {
      //Subtract elapsed time every frame
      totalTime -= Time.deltaTime;
    }
  }
}

With this, you have created a basic countdown timer in Unity. The timer will continue to run, as long as the total time is not equal to 0.

Trigger an action when the timer expires

It’s not enough to just create a countdown timer, an action should be triggered once the timer runs out. For example, you may want to display a message like "Time’s up". How can this be done in Unity?

This can be achieved with the use of else condition.

if (totalTime > 0)
{
  totalTime -= Time.deltaTime;
}
else
{
  Debug.Log("Time's up!")
  totalTime = 0
}

You will notice in the above code that the totalTime is set to 0 when the timer runs out. This is because the timer tends to be a negative number when it stops so it’s set to 0 to avoid this.

Now, how do we display the timer as a text in minutes and seconds?

Both the minutes and seconds has to be calculated from the float time value. This can be achieved with the FloorToInt method of the Mathf class in Unity.

To extract the minutes and seconds from the float time value;

//divide the time by 60
float minutes = Mathf.FloortoInt(totalTime / 60);

// returns the remainder
float seconds = Mathf.FloortoInt(totalTime % 60);

With the above code, you have successfully extracted the minutes and seconds from the float time value.

Now, we need to create a text object to display the timer. You can declare a public Text object in your script, like this:

public Text timerText;

Then, set the text property to the minutes and seconds formatted to string. Like this:

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

Complete example

Now, your whole code should look like this:

using UnityEngine;
using UnityEngine.UI;

public class CountdownTimer : MonoBehaviour
{
  public float totalTime = 90; //Set the total time for the countdown
  public Text timerText;

  void Update()
  {
    if (totalTime > 0)
    {
      // Subtract elapsed time every frame
      totalTime -= Time.deltaTime;

      // Divide the time by 60
      float minutes = Mathf.FloortoInt(totalTime / 60); 
      
      // Returns the remainder
      float seconds = Mathf.FloortoInt(totalTime % 60);

      // Set the text string
      timerText.text = string.Format(“{0:00}:{1:00}”, minutes, seconds);
    }
    else
    {
      timerText.text = "Time's up"; 
      totalTime = 0;
    }
  }
}

Now, you know how to create a basic countdown timer in Unity. It’s your turn to start using countdown timers in your projects.

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free