Timer Runtime Problem

I'm using a timer that uses time.time to count down from 60 seconds to 0. The problem is that:

A.) I can't add time, like to extend the count down.

B.) It runs even in the main menu.

Can anyone offer suggestions to remedy this problem?

Here's the code for now:

static var Timer: int = 0;
static var startTime: int = 120;
var timeLeft: int;
var prefix = "Time Left: ";
var suffix = " ";

function Awake()
{
Timer = startTime;
}

function Update () {

guiText.text = prefix + timeLeft + suffix;

Timer = Time.time * -1;

timeLeft = startTime + Timer;

if(timeLeft <= 0)
{
Application.LoadLevel ("Game Over");
}

}

To address the problems you're having, I would manually create a timer, rather than using Time.time.

private var timeRemaining : float = 60.0;

function Update()
{
   timeRemaining -= Time.deltaTime;
}

Using this approach:

  • you can subtract time in an Update loop you know will only get called when not on the main menu.
  • you can add to timeRemaining to extend the countdown.

If you'd rather use Time.time, you could use a few other variables to track bonus time and paused time, but I honestly think this way is simpler. Please ask if you have any questions!

Can you post your script? We can't help unless we can see what you wrote...