x


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?

more ▼

asked Dec 09 '11 at 01:53 AM

fireomega gravatar image

fireomega
356 5 7 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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 :)

more ▼

answered Dec 09 '11 at 04:59 AM

Aralox gravatar image

Aralox
76 6 7 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3808
x356
x268

asked: Dec 09 '11 at 01:53 AM

Seen: 389 times

Last Updated: Dec 09 '11 at 04:59 AM