Time for action – creating the countdown logic
Let's set the startTime
variable, and build the logic to handle the counting-down functionality of our clock.
Set the
startTime
variable:function Awake() { startTime = Time.time + 5.0; }
Note
Five seconds to beat the game is a bit ridiculous. We're just keeping the time tight for testing. You can crank this variable up later.
Decrease the amount of time on the clock:
function DoCountdown() { timeRemaining = startTime - Time.time; }
If the clock hits zero, pause the clock and call the
TimeIsUp()
function:timeRemaining = startTime - Time.time; if (timeRemaining < 0) { timeRemaining = 0; clockIsPaused = true; TimeIsUp(); }
Add some
Debug
statements so that you can see something happening:function DoCountdown() { // (other lines omitted for clarity) Debug.Log("time remaining = " + timeRemaining); } function TimeIsUp() { Debug.Log("Time is up!"); }
Save the script and test your game.
You should see the Debug
statements in...