C# Code:
Headerfiles:
Headerfile is used for acess the unity buildin functions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
Variables Initilize:
public float timerValue = 10; //Set your Timer Value
public bool timerRunning = false; //This boolis used for timer start/Stop.
public Text timerText; //This Text Is Used For Dsplay timer value.
Start Function:
private void Start()
{
// Starts the timer automatically On Load Scene
timerRunning = true;
}
Update Function:
void Update()
{
if (timerRunning )
{
if (timerValue= > 0)
{
timerValue -= Time.deltaTime;
DisplayTime(timerValue);
}
else
{
Debug.Log(“Time has run out!”);
timerValue = 0;
timerRunning = false;
}
}
}
Timer Display Function:
void DisplayTime(float timeToDisplay)
{
timeToDisplay += 1;
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format(“{0:00}:{1:00}”, minutes, seconds);
}
Complete Code:

Average Rating