Timer record

I’ve found a script for a timer for a racing game that keeps tells you how long you’ve been driving for. My problem is that I want to make it so that you can saved the time the person got. The timer is made by making GUI text and attaching this script:

var Timer = 0.0;

function Update ()
{
Timer += Time.deltaTime;

guiText.text = “” + Timer;
}

Is there anyway way to make it so that when a gameObject with the tag “Van” is destroyed that the timer either stops or gets recorded?

Here is a way: Have a ‘game controller’ GameObject that will be used to store the time, and create and attach a script to it with a public variable you can access to store the time.

Eg, have

public int vanTime;

in the gameController script.

Within the script attached to the Van, a simpler way of keeping track of the time is to record the time you started racing, eg inside the Start() function, have

timeStart = Time.time;

And when the van is destroyed, inside OnDestroy() (see list of the functions unity calls here)

timeElapsed = Time.time - timeStart;
GameObject.Find("gameController").GetComponent<gameControllerScript>().vanTime = timeElapsed;

Where the game controller GameObject is called “gameController” and the script attached to it is called “gameControllerScript”

You now have the Van’s time stored and available for use within the game controller script.

Good luck :slight_smile: