- Home /
 
The question is answered, right answer was accepted
Countdown timer?
Okay, I have the code for a countdown timer, I get how that works. My only problem is how would I have it countdown in a time format. So that, say I have 2 minutes, it'll go from 2:00 to 1:59, 1:58 and so on.
The code I'm using is C# so any code posted would be preferable in C#
Thanks ;)
http://answers.unity3d.com/questions/45676/making-a-timer-0000-$$anonymous$$utes-and-seconds.html
http://xeophin.net/en/blog/2010/10/18/how-format-time-string-c-unity-3d
http://answers.unity3d.com/questions/348742/time-formatting.html
http://answers.unity3d.com/questions/259482/format-string-$$anonymous$$utesseconds.html
Answer by violence · Dec 06, 2013 at 04:51 PM
I dont know C# so this is all pseudo code, it should be pretty easy:
Have your total amount of time be in seconds. Make sure your values are all ints, as this will all be integer division for ease of use with remainders.
minutesDisplay = seconds / 60
secondsDisplay = seconds - (minutesDisplay*60)
then print to a guiLabel (minutesdisplay + ":" + secondsDisplay)
you will need some conditional formatting for displaying a zero in front of the digit when the secondsDisplay number is less than 10
Here is a timer class in C#:
 public Class Timer:
 {
   public float timer;
 
   public Timer()
   {
     timer = 0;
   }
 
   public Timer(float startTime)
   {
     timer = startTime;
   }
 
   public void countUp(float seconds)
   {
     timer += seconds;
   }
 
   public void countDown(float seconds)
   {
     timer -= seconds;
   }
 
   public string toString()
   {
     return ((int)(timer/60)).toString() + ":" + ((int)(timer%60)).toString();
   }
 }