0
\$\begingroup\$

Kind of an easy question but everything I have found on this topic is either outdated or only counts down numbers and not time which is what I need. How can I go about creating a countdown timer where I can input a public float or int for time and then the text on screen will count it down to zero in a time format (00:00:00).

For example if I input "120" (or whatever the proper way to input time is) for instance, then the countdown timer would format correctly to "02:00" then start counting down by seconds. The tutorials ive seen just count down by numbers and dont take time into account (120,119,118, etc)

On top of that I am confused on how to implement a way to activate scripts at a certain time, for example at "1:00" left I would like to send a warning message or something. Please help and thanks!

\$\endgroup\$
1
  • \$\begingroup\$ I'm confused... Why would formatting a time string as a countdown in Unity be any different from doing it in any other kind of C# program? You remembered to search for existing solutions first, right? Please ask one question at a time. If you have a separate question about triggering a script at a set time, that should be posted as its own question. \$\endgroup\$
    – DMGregory
    Apr 11 '20 at 10:49
2
\$\begingroup\$

Keeping track of time

You likely already figured out that the best way to keep track of time left in Unity is to have a private float variable which gets initialized to the starting time and then gets reduced by Time.deltaTime in the Update method:

secondsLeft -= Time.deltaTime;

Creating a UI timer

But the problem is that Unity does not provide a comfortable way to format a number of seconds in a float-variable in a human-readable format. But fortunately there is a class for that in the C# standard library: The class TimeSpan.

TimeSpan timeSpanLeft = TimeSpan.FromSeconds(secondsLeft) ;

Create a text element on your UI canvas and set its text like this:

yourUiTextElement.text = timeSpanLeft.ToString();

The method ToString of TimeSpan by default gets you the time left in the format hh:mm:ss.ffffff. If you want to omit the milliseconds, you can also pass a format string to it. If you always want it in the format hh:mm:ss, you would call it like that:

yourUiTextElement.text = timeSpanLeft.ToString(@"hh\:mm\:ss");

Handle events when the timer reaches a certain value

A common beginner mistake is to write code like that and expect it to trigger reliable:

if(secondsLeft == 60.0f) { // <- WRONG!

Time.deltaTime skips ahead in unpredictable intervals, so while you can expect a value controlled by it to be larger or smaller than a constant, you can never expect it to be exactly equal to a constant. You must not check if there are exactly 60 seconds left, because it's more likely than not that the value will change from a little over 60.0 to a little under 60.0 between two updates. You can only check if there are 60 seconds or less left. And in order to only trigger the event once and not every single update after the 60 second mark, you also need a bool variable which keeps track of whether this event has already happened or not.

if(secondsLeft <= 60.0f && warningDisplayed == false) { 
     // display the warning message
     warningDisplayed = true;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.