How do you record time like a stop watch?

Hi , I am making a game in which i want to know some scripts.I want to know that how to record time like a stop watch and then save the time you have been so far and how and then a script in which press β€œR” button to respawn
please help , Thank you
:slight_smile:

Recording time is as easy as adding to a float variable. Everything around that is just booleans and functions to keep it simple.
Quick script which isn’t tested. Read through it and try to learn the basics from it.

static var record : boolean;
static var currentTime : float;
static var bestTime : float;

function Start () {
    // Reset current time
    currentTime = .0;

    // Start record
    StartRecord();
}

function Update () {
    // Keep adding to currentTime if record is true
    if (record)
        currentTime += 1*Time.deltaTime;

    // If user press R then run Respawn function
    if (Input.GetKeyDown(KeyCode.R))
        Respawn();
}

function Respawn () {
    if (!Application.isLoadingLevel)
        Application.LoadLevel(Application.loadedLevel);
}

static function StartRecord () {
    record = true;
}

// Call StopRecord(false) to pause record
// Call StopRecord(true) to end record and check if it's the best time
static function StopRecord (checkBestTime : boolean) {
    record = false;
    if (checkBestTime && currentTime<bestTime)
        bestTime = currentTime;
}

If you mean storing the value on disk then use PlayerPrefs, for example:

static function StopRecord (checkBestTime : boolean) {
    record = false;
    if (checkBestTime && currentTime<bestTime) {
        bestTime = currentTime;
        PlayerPrefs.SetFloat("Best Time", bestTime);
    }
}

sorry , but this didnt worked