- Home /
Duplicate Question
How to Format Timer Counting Up from Zero?
Hey everybody.
I have a timer that starts at zero and counts up during game play. As it gains value, I want to display the time in minutes and in seconds and if the timer gets high enough, hours too.
It just shows as seconds right now.
How would you go about doing this?
Any help or guidance is really appreciated.
Thanks in advance.
Look at the accepted (green) answer in this other topic ->
http://answers.unity3d.com/questions/653362/help-with-clock-script.html
I don't find the reason why the @robertbu closed this question. It's not a duplicated one. Here's the solution for this question
//GuiText to Show Time
public GUIText myTxt;
int hh=0,
mm=0,
ss=0;
void Start()
{
StartTimer ();
}
//Call this function to Start Timer
public void StartTimer()
{
StartCoroutine ($$anonymous$$yTimer());
}
//Call this function to Stop Timer
public void StopTimer()
{
StopCoroutine ("$$anonymous$$yTimer");
}
//Timer Logic
IEnumerator $$anonymous$$yTimer ()
{
for(;;)
{
myTxt.text = FormatTimer(hh)+" : "+FormatTimer(mm)+" : "+FormatTimer(ss);
yield return new WaitForSeconds(1);
ss+=1;
if(ss>59)
{
ss=0;
mm+=1;
}
if(mm>59)
{
mm=0;
hh+=1;
}
}
}
//Format Timer
string FormatTimer (int p)
{
if (p <= 9)
return "0" + p.ToString ();
else
return p.ToString();
}
I just typed out a fairly long explanation of what I figure to be @robertbu 's reason for closing this question, my thoughts on circumventing moderators decisions to close questions by posting answers in comments, etc etc... then I deleted it all because, simply, "Why bother?"