Timer issues

I have read a couple of other issues with timers but cannot find a soloution to my problem. At the moment i have a count down timer that displays on screen but when it gets to 0 it will not stop- it goes into the negative numbers. Can anyone see why it will not stop at 0? When it gets to 0 i would like the game to stop as well

private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

var countDownSeconds : int;

function Awake() {
    startTime = Time.time;
}

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

    restSeconds = countDownSeconds - (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");
        //do stuff here
    }

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

    text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Label (Rect (400, 25, 100, 30), text);
    }
function OnGUI () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
    var guiTime = Time.time - startTime;

    restSeconds = countDownSeconds - (guiTime);

Why would it stop ? There is no condition (if test), allowing or not the execution of the code.

    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
        //do stuff here
    }

As the comment say ( // do stuff here), you should put your code line there (what happens when the countdown goes to zero ?)

change if (restSeconds == 0) to if (restSeconds <= 0) and it should work. you should be careful when you use the == operator on floats, restSeconds won’t be exactly 0 in any OnGUI call.

I fixed it by adding:

if (restSeconds <= 0) {
        print ("Time is Over");
        
   		var guiTime1 = Time.time - stopTime;
   
    }

Can anyone help me as to why the game keeps going after the timer stops? I know its because i haven’t told it too but im not very good at java script so any tips or pointers will be appreciated

Sounds like you already know the answer:

Generally, whatever you want to have happen in your game, you must express in code (aside from things Unity take care of for you, like rigid body simulation and so forth). As for your question, the first step will be to specify what it means for the game to stop. Does player control cease? Does everything freeze? Does the world keep going while an in-game menu comes up? Does the screen fade out? Does a ‘game over’ message come up?

Once you’ve figured out what it is exactly that needs to happen, then you can move on to the next step, which is implementing it in code.

Thanks for that getting specific has helped me to know what i want.
I want the whole game to freeze, and fade to a black screen where a score is displayed.(i haven’t yet figured out how to implement the scoring system yet so i hope this doesn’t stop me from continuing with the timing script)

if you want to freeze the game set Time.timeStep to 0. this will stop physics and FixedUpdate calls.
you can stop everything else that needs to be stopped by simply disabling the behaviour.