Day 8 | Cooldown System in Unity

Many games implement cooldowns, a period of time that players must wait before repeating the same action, for player abilities. This article will cover how to implement a basic cooldown system in Unity.
Setup
The example project that I will use for this article will do the following:

In the clip, the following occurs.
- A CubeSpawner object spawns Cube prefabs on the left.
- The Cube prefab has a script that moves it to the right at a given speed.
- The prefabs are deleted once they reach a certain point on the right.
Completing the Setup is recommended but not necessary to learn the concept of how to implement a cooldown system. However, it’s helpful to have something hands-on to tinker with later.
Cooldown System Implementation
You may have noticed in the clip that multiple cubes were made. This is because Unity’s Update() method recorded the key input to make the Cube for multiple frames even though the input was tapped quickly. We want to make sure only 1 cube can be made at a given rate of time.
Pseudocode
This is where the cooldown system comes into play. Let’s write the idea of the cooldown system in pseudocode.
If the cooldown time for making the cube has elapsed
Make the cube
Reset the cooldown time.
Code Logic
In Unity, we keep track of time using the property Time.time. This property tracks the time elapsed since the game started.
We can use this property as a reference point for our cooldown system implementation. We’ll make a cooldownComplete variable to track when a cooldown has finished in terms of in-game time. Then, we ensure Time.time is greater than or equal to cooldownComplete before making the cube.
Continuing along with our pseudocode, we already have the “make cube” logic from our Setup, so now we have to reset the cooldown time.
After a cube is made, we’ll reassign cooldownComplete as the following:
cooldownComplete = Time.time + cooldownTime;
where cooldownTime is the amount of time you want users to wait before they can make another cube. Now we run an example through our logic:
- Say a cube was made at 2 seconds in-game time.
- This means Time.time was greater than or equal to cooldownComplete
- cooldownComplete is then reassigned as Time.time + cooldownTime. We’ll assume cooldownTime is 0.5s.
- Now, the Update() method is called, and when we check if Time.time ≥ cooldownComplete, this will keep returning false until Time.time ≥ 2.5s (Time.time + cooldownTime)
Try to merge this logic into your own make cube method before you look at the code below:

Here’s the result after you implement your solution:

Now the cubes are better spaced out when spawned. Hope this helps you on your journey to making great games!