Add seconds to timer countdown.

Okay so I have a pretty basic countdown timer here that counts down x # of seconds. It works to countdown:). The problem is I want to add x amount of seconds to the time when you hit a gameobject tagged timed. The problem is I get errors so something is not right. (I am still new to scripting)

private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var timed = false; 
private var displayMinutes : int;
var countDownSeconds : int;
function Awake()
{
startTime = Time.time;
}
function OnGUI ()
{
//  I need to make sure that my time is based on when this script was first called
//instead of when my 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)
{
Application.LoadLevel(loadedLevel);
//I may add more later
}
//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 OnControllerColliderHit(Hit : ControllerColliderHit)
{
    if(Hit.gameObject.tag == "fallout2")
    {
        timed = true;
    }
}

function LateUpdate()
{
    if(timed)
    {
    restSeconds == 60;
    timed = false;
    Destroy(OnControllerColliderHit.gameObject); 
    }
}   

That is my code. As seen above I also have a problem with resetting the level. It does not want to load. Man I feel stupid when it comes to scripting. Anyway thanks for your time. :)

Hi,

When you reset you should change this:

restSeconds = 60;

And about the LoadLevel error, you should define `loadedLevel` as :

var loadedLevel : int; 
loadedLevel = 1;//for example 1

Take a look to this. If you want to load the scenes as strings, a string is always between "Helo", for example:

var loadedLevel : String; 
loadedLevel = "NameoftheLevel";

You should change here, not inside the if() ;)

function LateUpdate()
{
    if(timed)
    {
    restSeconds = 60;
    timed = false;
    Destroy(OnControllerColliderHit.gameObject); 
    }
}  

That is some really complicated code for what you're trying to do. :-) Anyhoo, the basic problem here is that even though `restSeconds` appears to be a class variable, you overwrite its value in `OnGUI`, so it doesn't actually lead to any extra information being kept around.

Try this: remove `startTime` entirely, and make `restSeconds` a float. In `Awake()` (or, depending on what you want, in `Reset()`), set `restSeconds` to `countDownSeconds`. In `OnGUI()`, remove the `guiTime` variable and don't change `restSeconds`. (Also, for the one minute warning, use `RoundedRestSeconds`.) In `Update()` or `LateUpdate()`, decrement `restSeconds` by the value of `Time.deltaTime`. To increase the amount of time left, just add to restSeconds.

I figured it out! However I could not have done it without the help of Uzquiano so I am going to put him as the good answer. I changed the code so that when you hit the collider of the object tagged timed it resets the timer to 40 seconds (The starting time) Thank you all!:)