× Phaseshift FCOM Tutorials

Timers with Coroutines


Check this guide on how to create simple and performant timers.

Coroutines are great when you want to call a function after a set time. They can also be used for lerping and/or repeated function calls over a time period and are more performant than Invoke() and InvokeRepeating(). With Coroutines you could create a 3 second timed explosive on a stick of dynamite, a gently fading UI element that eventually disappears upon completion, or to spawn a roster of GameObjects in sequence over a time period. The following articles gives examples of all of these scenarios.

Basic Coroutine time out


This example uses WaitForSeconds() to wait for x seconds (in this case 3 as set by the float variable fuse), before calling the InstantiateExplosion() method. The StartCoroutine()m ethod should be called whenever you want to start the timer. This could be when receiving player input to throw dynamite.

This script is super useful when you want to create a simple, but effective timer. You can customise the ExplosionDelay() function to take a float delay argument that you could pass to WaitForSeconds(). This would give you greater flexibility with varying fuse times.

It's worth noting that...

Coroutines stop when the MonoBehaviour it's called from is destroyed or if the GameObject the MonoBehaviour is attached to is disabled. This means the Coroutine will not complete, and whatever function was called at the end of the timer will not get called. In some cases this may be expected behaviour! If you still want to call a function at the end of a timer on a potentially destroyed GameObject, you can use Invoke() or InvokeRepeating() as this will still call upon parent object destruction.

Basic Coroutine time out with Lerp


This example imagines you want to create a UI element that shows the players lap time upon crossing the finish line, for it to gradually fade out over time. You can call StartCoroutine() the same way as above.

In this example, lapTimePanel is a CanvasGroup object containing the lap time data. Once called it is set as Active, with it's Coroutine FadeLapTimePanel() run. While it is being run, lapTimePanel.alpha is gradually reducing linearly from 1 to 0 within timeout's 5 seconds to give a fading out effect.

Coroutine to trigger a function multiple times per x seconds


This code snippet solves a scenario where you want something to happen a set amount of times, but with a set delay between each trigger. This scenario assumes you have a strategy game where the player has requested several units to be built. This function allows this to happen with a set delay between the Unit exiting the factory.

This example is a little more complicated, but allows the player to create 5 units that spawn 2 seconds apart. delayBetweenSpawn is the amount of time in seconds between each unit spawn, and delay is initialised as a new WaitForSeconds() which will be doing the actual timer. When LaunchUnitFromFactoryWait() is called, it uses the quantity variable to set how many times to iterate through the while loop. When the first iteration of the while loop as completed, after the unitGroup.Spawn() method has been called and the index i incremented, WaitForSeconds() is called to temporarily pause the Coroutine execution, allowing for a delay as set by delayBetweenSpawn. When the loop index has exceeded the quantity requested, the timer will complete.

Documentation


To explore more on these topics, check out the documentation:

Coroutines
WaitForSeconds

Liked this article?

Please consider sharing!
Author

Josh Lyell

Game Developer