Unity How to Create Simple Timer C#
A visible or background timer runs on most games. We will create an easy one which ticks seconds. In this tutorial we don’t use any value such as integer, float etc. We just need a GUI text. Let’s create.
Open your project and create a GUIText. Right-Click on hierarchy window, UI/Text.
Edit its text. This step is important because we will use this text. Set it as your timer beginning value.
Now create a C# script. In this script, we are going to take the value of GUIText and decrease it by 1 every second. Don’t forget to implement using UnityEngine.UI; lib because Text needs it.
public class Timer : MonoBehaviour { public Text scoreText; void Start () { InvokeRepeating("RunTimer", 1, 1); } void RunTimer() { scoreText.text = (int.Parse(scoreText.text) - 1).ToString(); } }
That’s it. Set your UI text as scoreText and run it. We got a “seconds” timer with a simple logic.