O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required