HH:MM:SS game clock with modifiable start time error
I have implemented the following code. It used to work fine, but when I tried to add "startTime" the clock tries to display more than one seconds value at once. The startTime variable is set when the game is actually started (right now game begins but all players must tick in before it actually initialises everything). I would like to know what I've done wrong, and how to make it work perfectly, thanks!
Also, is there a reason why the timer GUI text is bold? When by default it is not?
time = Mathf.FloorToInt(Time.time) - Mathf.FloorToInt(startTime);
hours = time / 3600;
minutes = (time % 3600) / 60;
seconds = (time % 3600) % 60;
prettyTime = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
GUI.Label(new Rect(10, 10, 250, 100), prettyTime);
As you can see the values are overlayed.
Answer by Toon_Werawat · May 27, 2016 at 03:16 AM
Did you try TimeSpan With DateTime?
time = Time.time - startTime;
System.TimeSpan calc = System.TimeSpan.FromSeconds(time);
System.DateTime actualCalc = System.DateTime.MinValue.Add(calc);
prettyTime = actualCalc.ToString(@"hh:mm:ss");
GUI.Label(new Rect(10, 10, 250, 100), prettyTime);
But without DateTime you can just do this.
prettyTime = string.Format("{0}:{1}:{2}",calc.Hours,calc.Minutes,calc.Seconds);
prettyTime = calc.ToString("hh:mm:ss");
ERROR: no overload method ToString takes 1 argument?
@InappropriateFeelings Oh wait. I'm just little confused. It has to be.
System.TimeSpan calc = System.TimeSpan.FromSeconds(time);
System.DateTime actualCalc = System.DateTime.$$anonymous$$inValue.Add(calc);
prettyTime = actualCalc.ToString(@"hh:mm:ss");
This code does not solve the error. In fact it makes the hours value start at 12?
time = Time.time - startTime;
System.TimeSpan calc = System.TimeSpan.FromSeconds(time);
System.DateTime actualCalc = System.DateTime.$$anonymous$$inValue.Add(calc);
prettyTime = actualCalc.ToString(@"hh:mm:ss");
GUI.Label(new Rect(10, 10, 250, 100), prettyTime);
Just ditch DateTime and use calc ins$$anonymous$$d then.
prettyTime = string.Format("{0}:{1}:{2}",calc.Hours,calc.$$anonymous$$inutes,calc.Seconds);
time = Time.time - startTime;
System.TimeSpan calc = System.TimeSpan.FromSeconds(time);
prettyTime = string.Format("{0}:{1}:{2}", calc.Hours, calc.$$anonymous$$inutes, calc.Seconds);
GUI.Label(new Rect(10, 10, 250, 100), prettyTime);
This still doesn't work. The same error happens.