Making a simple timer in Unity(Unity timer simplified)

Timers are used in lot of places in game development. In Unity, timer can be accomplished with simple line of code. In this post we will see how you can make timers for different scenarios. I will also include codes that you can copy and directly use them in your game.

Remember the following before working with timers in Unity

  • Time works based on framerate and physics in Unity.
  • Any time parameter based on physics will have the term “fixed” in front of it.
  • Time.deltaTime returns a float.

Unity Timer- most useful inbuilt functions

  1. DeltaTime- Intervals between frames.
  2. FixedDeltaTime- Interval between physics frames.
  3. time- this the time passed from the start of the game.
  4. Fixedtime- time passed from the start of the game to the recent fixed update.
  5. timescale- the speed at which time passes in your game.

You can read about more Time functions here

FAQs on Unity timers

  1. How to make a simple countdown timer?

    Define a variable from which you want to start the countdown. Define it as an float as deltaTime returns a float. The next thing is to subtract deltaTime from your variable. Thats it, the code is added below for your reference.

  2. How to display timer in UI?

    First you need to create a UI element of type Text. Then assign the timer variable to the text variable using the ToString() function. You can see the code for example. You need to add “using UnityEngine.UI” for the Text variable.

  3. How to calculate Time elapsed in Unity?

    If you are looking for the time that has passed since the start of the game then, you can use the Time.time function to get it. If you are looking for the time since the level has loaded then you can use Time.timeSinceLevelLoad function.

  4. How to create slow motion effect in Unity?

    You can use the timescale function to control the speed at which time passes in your game. By default this value is set at 1, by changing this to a value between 0 and 1, you can create a slow motion effect. Some use this value to pause a game but you need to be careful as it only affects things inside Update or FixedUpdate functions.

  5. How to get time in Minutes or seconds in Unity?

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

Sample Unity timer Code

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine;
using UnityEngine.UI;

public class Timerexample : MonoBehaviour
{

float cntdnw = 30.0f;
public Text disvar;

void Update() 
{     
if(cntdnw>0)     
{         
cntdnw -= Time.deltaTime;     
}     
double b = System.Math.Round (cntdnw, 2);     
disvar.text = b.ToString ();     
if(cntdnw < 0)     
{         
Debug.Log ("Completed");     
} 
}
}

Let see what the above code does. “using UnityEngine.UI” is need to call the text variable. Without that it will throw an error.

  1. I declared a variable cntdnw and set its value to 30. It should be set as float because Time.deltaTime will return a float variable.
  2. Text variable disvar will be used to display the timer in your game.
  3. Inside the Update() function we are checking if the timer is greater than zero. If we don’t do this the timer will go into negative too.
  4. We are subtracting the time passed between every frame using the Time.deltaTIme function.
  5. Round-off the float to the number of decimal as required. We are doing it with the variable b.
  6. Then we are converting the float variable to string before assigning it to the display variable.
  7. We are checking if the timer has reached zero and then we can execute required code as per game needs.
Unity timer
Output 1
timer code output
Output 2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: