Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to create a countdown timer in a Unity game I am creating. Up until now my game was running for a specific time but I was using InvokeRepeating to end the game after the set time.

But now I want to display the time left in a GUI text. One approach could be to use InvokeRepeating again but this time call it every second to decrement the timer. With this approach though it would mean do extra calculations for when a minute passes in order to display time appropriately and not just display the whole time in seconds.

Is there a different approach? Does Unity have any timers built in? Also which is the more efficient method?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Alternatively, you could use System.Timers.Timer which is probably the most performant of any solution. The following example shows a countdown for 100 seconds.

System.Timers.Timer LeTimer;
int BoomDown = 100;
void Start ()
{
    //Initialize timer with 1 second intervals
    LeTimer = new System.Timers.Timer (1000);
    LeTimer.Elapsed +=
        //This function decreases BoomDown every second
        (object sender, System.Timers.ElapsedEventArgs e) => BoomDown--;
}

void Update ()
{
    //When BoomDown reaches 0, BOOM!
    if (BoomDown <= 0)
        Debug.Log ("Boom!");
}
share|improve this answer
1  
Do you know which of the three approaches is the most efficient? Yours, mine or return trues? –  John Demetriou May 17 at 6:57
1  
In order from most efficient to least: System.Timers.Timer, Coroutines, then finally InvokeRepeating. InvokeRepeating has the nasty habit of using reflection every time to call the function which is pretty slow. @Return True is right though. This is a premature optimization. I'd use Timers just because I find them easier to use but for something like an animation, coroutines are the way to go. –  JPtheK9 May 17 at 16:30
    
I guess I will follow the Timer approach then. They different implementations may not differ so much in efficiency at my game but it's important to stay efficient and use the most efficient practices –  John Demetriou May 18 at 14:45
1  
Oh, in terms of accuracy, Timer is the way to go. Coroutines use update frames and delta times for WaitForSeconds() while Timers use system ticks. If the FPS dropped to 2 FPS, the coroutine would also drop to a rate of 2 ticks per second (maximum) and would have a hard time being more accurate than .5 seconds. –  JPtheK9 May 18 at 17:17

Coroutines are you friend I think on this one...

http://docs.unity3d.com/Manual/Coroutines.html

Here is an example:

void Start () {
    StartCoroutine ("Countdown", 10);
}

private IEnumerator Countdown(int time){
    while(time>0){
        Debug.Log(time--);
        yield return new WaitForSeconds(1);
    }
    Debug.Log("Countdown Complete!");
}
share|improve this answer
    
Do you know which of the three approaches is the most efficient? Yours, mine or JPtheK9s? –  John Demetriou May 17 at 6:56
1  
The difference would be so negligible it would not be worth the time to even test it. I think this would best fall under premature optimization. I recommend using the one that works best for you, and that you can better understand and that lends itself to what you want it to do with it (for instance, if you wanted to load and wait on data from a website, Coroutine is most definitely the better way to go, whereas in other instances using Timer is a life saver). I honestly think you should learn to use both, and include both methodologies in the future when developing with Unity. Hope that helps –  return true May 17 at 7:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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