- Home /
making a timer (00:00) minutes and seconds
Ok, so Im trying to make a game timer that increments every second and displays it in the format of MM:SS
Heres what I have
public static float timer;
public static bool timeStarted = false;
void Update ()
{
if (timeStarted == true)
{
timer += Time.deltaTime;
}
}
void OnGUI()
{
float minutes = Mathf.Floor(timer / 60);
float seconds = timer%60;
GUI.Label(new Rect(10,10,250,100), minutes + ":" + Mathf.RoundToInt(seconds));
}
but it outputs M:S (if the seconds is over 10 then obviously the seconds will be double digit) would anyone be able to explain where im going wrong?
Answer by LoTekK · Feb 01, 2011 at 06:40 PM
I would imagine there's a cleaner way to do this, but:
float minutes = Mathf.Floor(timer / 60);
float seconds = Mathf.RoundToInt(timer%60);
if(minutes < 10) {
minutes = "0" + minutes.ToString();
}
if(seconds < 10) {
seconds = "0" + Mathf.RoundToInt(seconds).ToString();
}
GUI.Label(new Rect(10,10,250,100), minutes + ":" + seconds);
Here I'm just converting the float to a string if the value is less than 10, and sticking a leading zero on. Also, performing the RoundToInt at the variable definition instead of in the GUI.Label call. Haven't had time to actually test it, but that should work.
The cleaner way of doing it is use the built-in option for custom `ToString()` formats as such:
float $$anonymous$$utes = $$anonymous$$athf.Floor(timer / 60).ToString("00");
float seconds = (timer % 60).ToString("00);
This will force the number to always display at least two-digits even when their value is lower than 10, replacing the first digit with a 0. When using the similar format of "#0" the first digit will only show if the number's value is greater than or equal to 10.
Right...:
void Update(){
float timer += Time.deltaTime;
string $$anonymous$$utes = $$anonymous$$athf.Floor(timer / 60).ToString("00");
string seconds = (timer % 60).ToString("00");
print(string.Format("{0}:{1}", $$anonymous$$utes, seconds));
}
You can't cast float
to string
. Codes don't compile at all.
That's what asafsitner wanted to type:
string $$anonymous$$utes = $$anonymous$$athf.Floor(timer / 60).ToString("00");
string seconds = (timer % 60).ToString("00");
You'll want to add a $$anonymous$$athf.Floor to the seconds as well, if to prevent a display bug where you see 00:60 for half a second. (It rounds the displayed seconds up to 60 from 59.9999 - 59.5)
string $$anonymous$$utes = $$anonymous$$athf.Floor(timer / 60).ToString("00");
string seconds = $$anonymous$$athf.Floor(timer % 60).ToString("00");
I have a method in C# that has some code formattings implemented if anyone will find it useful:
public string FloatToTime (float toConvert, string format){
switch (format){
case "00.0":
return string.Format("{0:00}:{1:0}",
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*10) % 10));//miliseconds
break;
case "#0.0":
return string.Format("{0:#0}:{1:0}",
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*10) % 10));//miliseconds
break;
case "00.00":
return string.Format("{0:00}:{1:00}",
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*100) % 100));//miliseconds
break;
case "00.000":
return string.Format("{0:00}:{1:000}",
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*1000) % 1000));//miliseconds
break;
case "#00.000":
return string.Format("{0:#00}:{1:000}",
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*1000) % 1000));//miliseconds
break;
case "#0:00":
return string.Format("{0:#0}:{1:00}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60);//seconds
break;
case "#00:00":
return string.Format("{0:#00}:{1:00}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60);//seconds
break;
case "0:00.0":
return string.Format("{0:0}:{1:00}.{2:0}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*10) % 10));//miliseconds
break;
case "#0:00.0":
return string.Format("{0:#0}:{1:00}.{2:0}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*10) % 10));//miliseconds
break;
case "0:00.00":
return string.Format("{0:0}:{1:00}.{2:00}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*100) % 100));//miliseconds
break;
case "#0:00.00":
return string.Format("{0:#0}:{1:00}.{2:00}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*100) % 100));//miliseconds
break;
case "0:00.000":
return string.Format("{0:0}:{1:00}.{2:000}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*1000) % 1000));//miliseconds
break;
case "#0:00.000":
return string.Format("{0:#0}:{1:00}.{2:000}",
$$anonymous$$athf.Floor(toConvert / 60),//$$anonymous$$utes
$$anonymous$$athf.Floor(toConvert) % 60,//seconds
$$anonymous$$athf.Floor((toConvert*1000) % 1000));//miliseconds
break;
}
return "error";
}
Answer by jashan · Sep 08, 2012 at 06:45 PM
Actually, there's a much nicer way of doing this:
void OnGUI() {
int minutes = Mathf.FloorToInt(timer / 60F);
int seconds = Mathf.FloorToInt(timer - minutes * 60);
string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
GUI.Label(new Rect(10,10,250,100), niceTime);
}
This will give you times in the 0:00 format. If you'd rather have 00:00, simply do
string niceTime = string.Format("{0:00}:{1:00}", minutes, seconds);
There's a couple of possibilities you have with the formats here: {0:#.00} would give you something like 3.00 or 10.12 or 123.45. For stuff like scores, you might want something like {0:00000} which would give you 00001 or 02523 or 20000 (or 2000000 if that's your score ;-) ). Basically, the formatting part allows any kind of formatting (so you can also use this to format date times and other complex types). Basically, this means {indexOfParameter:formatting}.
i have to say thank you .....ive been working on this problem for days going crazy and this solution works great i was jumping up and down for join when i saw it working....thank you
Hi @jashan Where should I place this script? I use s$$anonymous$$mVR, and placing it on the cameraRig- eyes will not worl for me.
Wow this works great, but when I reload the scene it doesn't reset. Any idea why?
string niceTime = string.Format("{0:00}:{1:00}", $$anonymous$$utes, seconds);
It says that $$anonymous$$utes and seconds don't exist in the context, do I still need to add the int $$anonymous$$utes part? I thought it declared it in that but I could be wrong
Answer by YasinJavaid_ · Jul 15, 2016 at 11:15 AM
sorry guys its late but single line solution is here
(totalTimeSec / 60 ).ToString("00") + ":" + (totalTimeSec%60).ToString("00");
or
Mathf.Floor(totalTimeSec / 60 ).ToString("00") + ":" + Mathf.FloorToInt(totalTimeSec%60).ToString("00");
try 2nd solution.
You forgot the $$anonymous$$athf.Floor (With 31 seconds, with your solutions shows 01:31)
$$anonymous$$athf.Floor(totalTimeSec / 60 ).ToString("00") + ":" + $$anonymous$$athf.FloorToInt(totalTimeSec%60).ToString("00");
Answer by Stardog · Jun 02, 2013 at 10:49 AM
Here is the example burnumd mentioned:
public float timer;
public string timerFormatted;
void Update()
{
System.TimeSpan t = System.TimeSpan.FromSeconds(timer);
timerFormatted = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
}
Hey Stardog,
You can just go t.ToString("hh:mm:ss:fffffff");
Regards,
Answer by burnumd · Feb 01, 2011 at 07:32 PM
Look into using C#'s string.Format. I'd recommend looking at this StackOverflow exchange which explains using it to display a time however you want it using a TimeSpan which you can create by calling new System.TimeSpan.FromSeconds (timer)
.
i thought string.Format was js only because when i tried typing it in c# in monodev it was throwing up error, any reason why? ive check out that link though thanks!
There could be lots of reasons why it threw up an error. Can you tell me what error you're getting?
Your answer
![](https://koobas.hobune.stream/wayback/20220613102251im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Turn seconds into minutes and seconds MM:SS 1 Answer
How to stop a Countdown Timer? 1 Answer
Countdown Timer Help About putting 0's 1 Answer
Making a timer 1 Answer
why the debree timer float not work 1 Answer