Timer problem (35166)

i need to find a simple way to write 0:00:00 into a gui text format with each 0 behind : being a different varible. so basically hours minutes and seconds.

i can do all this and tell it every 60 seconds add one to minutes and so on, but the one thing i cant seem to do is write 0:00:00. i can write it as numbers beside one another but i need the : between them which i just cant do

i did try something like

GUIText.text = “” +Hour+Minutes+Seconds;

but i cant seem to fit : anywhere any ideas

2 Answers

2

While it works this way too, it’s not very clean, flexible or even readable.

usually you will use string format functions for this.

For example, if your minutes are in 1-digit area, the result of 5 hours, 3 minutes and 5 seconds would be: “5:3.5” which is not something you want.

Instead use string formating.

guiText.text = string.Format("{0:D2}:{1:D2}.{2:D2}", Hour, Minutes, Seconds);

D tells it that it’s a digit and 2 the number how often.

So guiText.text = string.Format("{0:D5}", 5); will output 00005 (since 5 is 1-digit, the remaining will be filled with zeros)

so whats a different way of getting the real time without using Time.time as i need the time to start and stop at diferent places. at the moment im using yield wait for seconds and then add one.. then if a number reaches a certain number add one to the next and set back to 0.

–

Didn't checked out answers for a while. Usually you'd remember the time when you start the timer. ie. public void StartTimer() { startAt = Time.time; } And then just calculate the difference: float difference = Time.time - startAt; That's the milliseconds that passed since you started. Now you just have to format it (divide through 1000 for seconds, divide that through 60 for minutes etc.)

–

dont matter i got it i was missing out the extra + between each :

guiText.text = Hour+“:”+Minutes+“:”+Seconds;