- Home /
Countdown Through a Label?
Is it possible to have a label and have it countdown in seconds and then have it restart that timer after that time was used up? Im using this for a pay day type thing and this is the last thing i need in it. NOTE: i use c# and i plan on adding this to a current script i already have. If it is please respond and all help is appreciated.
thanks u could have posted that as an answer and i would have accepted it.
Answer by Ejlersen · Dec 30, 2010 at 06:28 PM
Another silly way of doing it. Probably not the best conversion to string method, but it works :)
public class CountDown : MonoBehaviour { public float t = 180.0f; // Seconds
void Update() { t -= Time.deltaTime;
if (t < 0.0f)
t = 180.0f;
}
private string ConvertTimeToString() { int hours = Mathf.FloorToInt(t / 3600); int minutes = Mathf.FloorToInt((t % 3600) / 60); int seconds = Mathf.FloorToInt(t) % 60;
return hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00"));
}
void OnGUI() { GUI.Label(new Rect(32.0f, 32.0f, 128.0f, 32.0f), ConvertTimeToString()); } }
works great but its set to 2:50 and not 3:00 and looking at that script i cant find out what to change without breaking everything else.... i do see the variable but the problem is that its set to 10000.0f and i only need it to be 180.0f but if that is 2:45 than i need to raise it but what should i raise it to?
The time is in seconds. 10000sec = 2h 46m 40sec If you want 3m = 180sec that would be right. I don't know why you subtract 50 per sec? you should subtract only deltaTime
and the string format can be done with hours.ToString("00") + ":" + $$anonymous$$utes.ToString("00") + [...]
Well, if you want it to show 3 $$anonymous$$utes, then just set it to 180.0f. Oh, I had some debug code in it, that made the timer go 50 times faster than normal. Just remove the * 50.0f, as I have done in the code now.
Your answer
Follow this Question
Related Questions
When Score goes up by 25 change timer value 1 Answer
Countdown timer trouble? 1 Answer
Countdown to start level 1 Answer
Timer doesn't work properly 2 Answers
end game condition 1 Answer