Unity Stopwatch Tutorial

In this tutorial, we will see how to create a simple Unity stopwatch with Start, Stop and Reset button and how to set up the UI required for it.

A stopwatch is not that different from a regular Unity timer. The only thing is you need is a start, stop, and a reset option for a stopwatch. You can do that simply with the help of Boolean variables.

If you are looking to make a countdown timer, then you can check out our other post on Unity timer.

Creating a Stopwatch in Unity

Let’s see the code for a stopwatch.

using UnityEngine;
using UnityEngine.UI;

public class Timerexample : MonoBehaviour
{

    float val;
    bool srt;
    public Text disvar;

    void Start()
        {
            val=0;
            srt=false;
        }

    void Update() 

    { 

        if(srt)     

            {         
                val += Time.deltaTime;    
            } 

        double b = System.Math.Round (val, 2);     

    disvar.text = b.ToString ();    
    }
    public void stopbutton()
    {
        srt=false;
    }   
    public void resetbutton()
    {
        srt=false;
        val=0;
    }
    public void startbutton()
    {
    srt=true;
    }
}

Create an empty gameobject and name it as stopwatch.

Attach the above script to your stopwatch gameobject.

Create 3 new UI buttons by going to Create>UI>Button in the hierarchy window.

Name them Start, Stop and Reset.

Select each button and go to the inspector properties. Click on the + sign in the OnClick event. Drag and drop the stopwatch game object and then select the respective function depending on your button

Adding unity timer script to button

Create a text UI and assign it to the stopwatch script in the inspector window.

Here is how the output looks like

Unity stopwatch timer output gif

Displaying Unity Stopwatch time in Minutes and seconds

In our above examples the time is displayed in decimal format but the actual time should be displayed in Minutes:Seconds

Unity timer in Minutes:Seconds format

We need to add two more Text UI to our scene. One will be for minutes and the other will be for seconds.

You can name them minutes and seconds.

Now let’s go back to the script and add two more public text variables.

public Text Minutes;
public Text Seconds;

In case of Text mesh pro the data type will be TMP_Text.

Mathf has a function called FloortoInt which we can use to get the minutes and seconds.

Then we can assign those values to the text variables.

Unity Stopwatch C# script with Seconds and minutes display

using UnityEngine;
using UnityEngine.UI;

public class Timerexample : MonoBehaviour
{

float val = 0;
bool srt;
public Text Minutes;
public Text Seconds;
 void Start()
        {
            val=0;
            srt=false;
        }
 void Update() 
 {     
  if(srt)     

            {         
                val += Time.deltaTime;    
            } 
  
  float minutes= Mathf.FloorToInt(val /60);  
  Minutes.text=minutes.ToString();
  float seconds=Mathf.FloorToInt(val%60);
  Seconds.text=seconds.ToString();
  
}
}

If you have any other questions regardign stop watch then feel free to drop them in the comment box below.

Leave a Reply

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

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading