Menu Close

Unity timer in minutes and seconds | How to make a timer on unity | Timer in unity C#

1 0
Read Time:40 Second

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:

Download Code:

Link 01:

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Related Posts

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: