Don’t Overheat — Cooldown with Unity

Mike Brisson
2 min readMar 31, 2021

You don’t want your program to be capable of over instantiating game objects. This isn’t a wise practice and can wreak havoc on your performance.

The way you can keep track of this is with one of Unity’s built in utilities such as Time.time:

Time.time tracks the amount of time that has passed since the game was started.

If you set a variable to track the last time an object was fired, you can then compare it like this:

In this scenario, we are comparing the amount of time that has passed in game against the variable which holds the last time we fired.

Here is an example of what was used in the title image:

I’m comparing “Time.time to “_lastFired”. If the statement is true, the laser is being created and then “_lastFired” is being updated to the current time + “_cooldownDelay”. The larger the delay, the longer the player will have to wait until he/she can fire again.

--

--