Resetting a countdown timer

I have a countdown timer which, once it reaches 0:00, it executes and Application.LoadLevel and loads a different scene. The only problem here, is that if I go back to the previous scene, the timer is still going. It doesn’t stop, it keeps going into the negatives. How can I stop it once it reaches 0 and reset it if the scene gets reloaded??

var startTime : int;
var restSeconds : int;

var roundedRestSeconds : int;
var displaySeconds : int;
var displayMinutes : int;

var countDown : int;


function Update () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
	startTime = 12;
	
    var guiTime = Time.time - startTime;

    restSeconds = countDown - (guiTime);

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
		MouseFollow.playerScore = 0;
        Application.LoadLevel(2);
    }
	

    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    guiText.text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
    //GUI.Label (Rect (350, 25, 100, 30), text);
}

Thanks.

Where is that script from? If you got it from somewhere, are you using it in its original form, or have you modified it?

I’d recommend stepping through the code using some example values, keeping in mind that Time.time is the time in seconds since the ‘beginning of the game’ (which I assume means since play mode was entered in the editor, and since the application was launched in a build).

I don’t see ‘countDown’ being initialized anywhere, so I’m assuming it always has a value of 0. ‘startTime’ always has a value of 12, which means the second and third lines of code in your Update() function reduce to:

var guiTime = Time.time - 12;
restSeconds = -guiTime;

So it looks to me like the timer will count down from 12 and on into the negatives regardless of whether you switch to another scene and back (just as you’re observing). In short, if you want it to behave differently, you’ll need to code it differently.