Time for action - create the countdown logic

Let's set the startTime variable, and build the logic to handle the counting-down functionality of our clock.

  1. Set the startTime variable:
    function Start() {
    startTime = 5.0;
    }
    

    Note

    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.

  2. Decrease the amount of time on the clock:
    function DoCountdown()
    {
    timeRemaining = startTime - Time.time;
    }
    
  3. If the clock hits zero, pause the clock and call the TimeIsUp() function:
    timeRemaining = startTime - Time.time;
    if (timeRemaining < 0)
    {
    timeRemaining = 0;
    isPaused = true;
    TimeIsUp();
    }
    
  4. Add some Debug statements so that you can see something happening:
    function DoCountdown() { ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.