- Home /
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?
Answer by Aralox · Dec 09, 2011 at 04:59 AM
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 :)
Your answer
Follow this Question
Related Questions
Timer Pause 1 Answer
Time.deltaTime appears to be running fast 1 Answer
Pause at end of game 2 Answers
Transperent GUI Text? 3 Answers
How can I make a quest system? 2 Answers