Reset the Time.time back to 0.0?

Hi I have a zombie like game and has a few scenes a title, the game, instructions, and scene after you died. In the game i have an extremely simple timer script that shows how long you have survived using Time.time. When you die you get taken to a scene that shows how long you have survived, a try again GUI.button, and a return to title GUI.button. If you click the try again one the time will not reset it will stay where you left off. Is there a simple way to reset it back to 0. I have tried making a boolean that is true if you clicked try again and in the timer script if it is true set the time to 0.0 but the time will just stay at zero because i didn’t make it false again. Please help.

The Timer script

static public var time : float;

function Update(){
time = Time.time;
}

function OnGUI(){
GUI.Label (Rect (52,85, 200, 200), "Time = "+ time);
}

The Death scene script

#pragma strict

var customSkin : GUISkin;
var timeAlive : float;

function Update(){
timeAlive = Timer1.time;
}

function OnGUI () {
var buttonWidth : int = 400;
var buttonHeight : int = 150;

var halfScreenWidth : float = Screen.width / 2;
var halfButtonWidth : float = buttonWidth / 2;

GUI.skin = customSkin;

if(GUI.Button(Rect(halfScreenWidth - halfButtonWidth, 150, buttonWidth, buttonHeight), "Try Again")){
	Application.LoadLevel("ZombieHunt");
}

if(GUI.Button(Rect(halfScreenWidth - halfButtonWidth, 450, buttonWidth, buttonHeight), "Title Screen")){
	Application.LoadLevel("TitleZombie");
}

GUI.Label(Rect(halfScreenWidth - 250, 45, 500, buttonHeight), "You Survived for "+timeAlive+" seconds");

}

please help also if you have any suggestions to make it better
I have played with unity for a while and know most things but not all.

You can’t reset Time.time, but I do see two options that will work just as well.

First option, probably the simplest, is to just use Time.timeSinceLevelLoad:

function Update() {
    time = Time.timeSinceLevelLoad;
}

Second option, a little bit harder but lets you start the timer whenever you want, is to use Time.time with an offset:

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

function Update() {
    elapsedTime = Time.time - startTime;
}

Time.time is the time from the start of the game. It’s read only and it will only reset when the game restarts.

Store your own time as a float. If you add Time.deltatime to it each frame it will count in seconds and you can set it back to zero whenever you like.

I provided three optional methods. The concept is whether you want to have a timer, reset a timer or stop a timer . Hope this helps someone, Cheers!

    public float timer = 0.0f;
    public int seconds;
    public bool keepTiming = true;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Timer();
        //ResetTimer();
        //StopTimer();
    }
    
    public void Timer()
    {
        // seconds
        timer += Time.deltaTime;
        // turn float seonds to int
        seconds = (int)(timer % 60);
        print(seconds);
    }

    public void ResetTimer()
    {
        // seconds
        timer += Time.deltaTime;
        // turn float seonds to int
        seconds = (int)(timer % 60);
        print(seconds);

        if (seconds > 4)
        {
            // both code runs the same
            //timer = Time.deltaTime;
            timer = 0.0f;
        }
    }

    public void StopTimer()
    {
        if (keepTiming)
        {
            // seconds
            timer += Time.deltaTime;
            // turn float seonds to int
            seconds = (int)(timer % 60);
            print(seconds);
        }

        if (seconds > 4)
        {
            // stop and record time
            keepTiming = false;
            print(seconds);
        }
    }

I don’t get the thing ! why do we have to subtract it from 0 (start time) what difference will it make ?