Count up timer for unity3d in minutes,seconds and milliseconds c#

I have been asked about how to make a count up timer in unity so many times, that i finally decided to share a simple script that does that, so here it goes.

this script will also format the timer to “00:00:00”

create a C# script in unity and add the following code to it:

float minutes = 0f;
float seconds = 0f;
float milliseconds = 0f;
string minutesS =””;
string secondsS = “”;
string millisecondsS = “”;

void Update(){

if(milliseconds >= 100){
if(seconds >= 59){
minutes++;
seconds = 0;
}
else if(seconds < 59){
seconds++;
}
milliseconds= 0;
}
milliseconds+= Time.deltaTime * 100;
if (minutes < 10) {
minutesS = “0” + minutes;
}
else {
minutesS = “” + minutes;
}

if (seconds < 10) {
secondsS = “0” + seconds;
}
else {
secondsS = “” + seconds;
}

if ((int)milliseconds < 10) {
millisecondsS = “0” + (int)milliseconds;
}
else {
millisecondsS = “” + (int)milliseconds;
}

Debug.Log(string.Format(“{0}:{1}:{2}”, minutesS, secondsS, millisecondsS));
}