Skip to main content

How to Create a Timer or Stopwatch in Unity

How to Create a Timer or Stopwatch in Unity

Timers and stopwatches are essential tools for many games, and Unity makes it easy to create them. In this tutorial, we will show you how to create a simple timer or stopwatch in Unity.

Steps to follow:

  1. Create a new Unity project.
  2. Create a text object in the hierarchy.
  3. Create a new script called Timer and attach it to the text object.
  4. In the Timer script, create two public variables: timeRemaining and timeIsRunning.
  5. The timeRemaining variable will store the amount of time remaining on the timer, and the timeIsRunning variable will be a boolean that determines whether the timer is running or not.
  6. Create an Update function in the Timer script.
  7. In the Update function, check if the timeIsRunning variable is true and if the timeRemaining variable is greater than zero.
  8. If both of these conditions are true, subtract the time elapsed since the last frame from the timeRemaining variable.
  9. Create a displayTime function in the Timer script.
  10. In the displayTime function, calculate the minutes and seconds for the timer.
  11. Update the text object with the current time.
  12. Drag and drop the Timer script into the time attack and add the text from the hierarchy.

Here is the code for the Timer script:

C#
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timeIsRunning = false;
    public Text timerText;

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

    void displayTime()
    {
        int minutes = Mathf.FloorToInt(timeRemaining / 60);
        int seconds = Mathf.FloorToInt(timeRemaining % 60);
        timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

Here is the code for the StartTimer function:

C#
public void StartTimer()
{
    timeIsRunning = true;
}

Here is the code for the StopTimer function:

C#
public void StopTimer()
{
    timeIsRunning = false;
}

Here is the code for the ResetTimer function:

C#
public void ResetTimer()
{
    timeRemaining = 10;
    timeIsRunning = false;
}

You can now use the StartTimer, StopTimer, and ResetTimer functions to start, stop, and reset the timer.

Here are some additional tips:

  • You can change the initial value of the timeRemaining variable to set the duration of the timer.
  • You can add a button to start and stop the timer.
  • You can add a sound effect to play when the timer runs out.
  • You can add a visual effect to indicate that the timer is running out.

I hope this tutorial helps you create a timer or stopwatch in Unity.

Comments

Popular posts from this blog

Mario in Unity 6 (2025) | Part 2: Adding Player Movement

  Mario in Unity 6 (2025) | Part 2: Adding Player Movement Welcome to Part 2 of our Mario in Unity 6 series! In the last part, we set up our project and created the player and ground sprites. Now, it’s time to breathe life into Mario by implementing player movement with C#. What’s Covered in Part 2: Writing a C# script to handle player movement. Assigning the script to the player object. Adding basic left, right, and jump mechanics. Fine-tuning the Rigidbody2D for smooth movement. Let’s dive in! Writing the Player Movement Script We’ll start by creating a C# script to handle movement logic: In Unity, navigate to the Project Panel . Right-click and select Create > C# Script . Name the script PlayerMovement . Double-click the script to open it in your code editor (e.g., Visual Studio). Paste the following code: using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed = 7f; public float jumpForce = 12f; public LayerMask ...

Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump)

  Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump) Animations are what make a game truly come alive. In Part 3 of this series, we’ll add animations to Mario for idle, running, and jumping states using Unity 6. This step will transform our simple character movement into a polished and interactive experience. By the end of this tutorial, you’ll learn how to set up animations, configure the Animator Controller, and write C# scripts to handle animation transitions based on player input. What Are We Doing in Part 3? Importing already-sliced Mario sprites for animation. Creating animations for idle, running, and jumping. Setting up an Animator Controller. Writing scripts to control animation transitions dynamically. Step 1: Importing the Sprite Sheet 1.1. Import the Sprite Sheet Download File Download the pre-sliced Mario sprite sheet provided in this tutorial. Drag the sprite sheet into your Unity project’s Assets folder. Since the sprite s...