How to make a countdown timer in Unity
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.