Videogames Development – Unity – How to create a countdown
Assign this code to an Empty Object in the Scene:
#pragma strict var endTime : float; function Start() { endTime = Time.time + 60; } function Update() { var timeLeft : int = endTime - Time.time; // We do not need negative time if (timeLeft < 0) timeLeft = 0; Debug.Log(timeLeft); }
Take it from the top:
1. Declare ‘endTime’ variable
2. inside ‘Start()’, setup inital value ‘endTime’=’Time.time’ (this is the time in seconds since the start of the game) + 60 -> 60 seconds
3. inside ‘Update(), ‘timeLeft’ = 60 – ‘Time.time’ (this is the time in seconds since the start of the game) -> 59…58…57
if (timeLeft < 0) timeLeft = 0; we do not need negative time
show inside debug console 'timeLeft'