Unity3D How To Create A Simple Countdown Timer

Unity3D How To Create A Simple Countdown Timer

I needed a simple countdown timer to create a splash screen effect so I could jump to the next scene. In this example, I create a time governor that loops and keeps checking to see if the time has expired ( <= 0 ) then executes the actions.

Set Total Countdown Time

In your class create a float variable that will be the total amount of time to countdown and the reset time holder:

public class CountDownTimer : Monobehaviour {

float timeRemaining; // the timer
float timeRemainingReset; // the timer reset

Add To A Main Update Loop

Add this code to any of your main update loops (Update, FixedUpdate, LateUpdate):

void Update() {
     timeRemaining -= Time.deltaTime; // subtract time each frame

     if( timeRemaining <= 0 ) {
          // reset the timer
          timeRemaining = timeRemainingReset;
          // perform your actions (load next scene, move a gameobject, perform garbage clean up etc.)
          HealPlayer();
          CheckCombat();
     }
}

Unity3D How To Create A Simple Countdown Timer