x


How to make a timer in the game

How to make a timer in the game?

What I have so far? I have a main menu, when you click start game it goes to another scene and you are in a maze.

What I need? What I need is once it enters the other scene to have a timer on the bottom of the screen to see how long it takes to finish.

Any ideas?

Thanks, Chris

more ▼

asked Feb 13, 2010 at 07:00 PM

cmos gravatar image

cmos
557 75 72 84

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

5 answers: sort voted first

This should get you started:

private var startTime;
var textTime : String; //added this member variable here so we can access it through other scripts


function Awake() {

   startTime = Time.time;

}

function OnGUI () {

   var guiTime = Time.time - startTime;

   var minutes : int = guiTime / 60;
   var seconds : int = guiTime % 60;
   var fraction : int = (guiTime * 100) % 100;

   text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); 
   GUI.Label (Rect (400, 25, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already

}

In your OnTriggerEnter function you then have to write the following to access the time when the trigger was entered (adjust "Main Camera" and "Timer" to whatever your game object and script name are that contain your timer):

function OnTriggerEnter (other : Collider) {
 var timeTrigger = gameObject.Find("Main Camera").GetComponent("Timer").textTime;
 Application.LoadLevel (1);//or whatever the number of your new level is
}
more ▼

answered Feb 13, 2010 at 08:42 PM

Sebas gravatar image

Sebas
4.7k 21 24 56

Ahh, Askhan beat me to it, but I suppose I leave it up for the formatting and stuff.

Feb 13, 2010 at 08:42 PM Sebas

Thanks, this worked great!

Feb 14, 2010 at 05:31 AM cmos

Hey Again..... is there any way to retrive the value of the Label from another script I am running. Basically, I want to know the time when i step on a block.

Feb 15, 2010 at 12:56 AM cmos

I edited the answer to answer your additional question. You add a member variable in your timer script so you can access it from other scripts. You access this variable by finding the game object and script which contains this variable. You do this in your OnTriggerEnter function. More information on this topic can be found in the script reference (http://unity3d.com/support/documentation/ScriptReference/index.MemberVariables26GlobalVariables.html) or the chapter on game objects and components under the same link

Feb 15, 2010 at 02:07 AM Sebas

I realise this is quite old, but the line with String.Format is assigning to 'text', a variable that doesn't exist.

Sep 26, 2010 at 09:08 AM Marowi
(comments are locked)
10|3000 characters needed characters left

you should get the time.time in a awake or start function in new scene and then subtract the current time.time from the first value. somethings like this

var t,ellapsed;
function Start ()
{
t=time.time;
}
function Update ()
{
ellapsed=time.time-t;
}
more ▼

answered Feb 13, 2010 at 08:30 PM

Ashkan_gc gravatar image

Ashkan_gc
10k 76 91 160

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

can i noe where to place all these method at in unity?

more ▼

answered Oct 05, 2010 at 01:58 PM

blueice gravatar image

blueice
1

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

Plz how to make this timer reversed ? For example from 30.00 to 0... Thanks.

more ▼

answered Nov 03, 2012 at 08:30 AM

cirus gravatar image

cirus
1 3 2 2

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

I know the topic is kinda old, but in case anyone needs it...

For a reversed time I guess you could initialize a variable "goalTime" with the current time plus the amount of seconds you want to wait.

I just used Sebas answer and modified a little bit.

private var goalTime;
private var totalTime = 30.0; //the amount of time you want to start the countdown;
var textTime : String; //added this member variable here so we can access it through other scripts


function Awake() {

   goalTime = Time.time + totalTime;

}

function OnGUI () {

   var guiTime = goalTime - Time.time; // You probably want to clamp this value to be between the totalTime and zero

   var minutes : int = guiTime / 60;
   var seconds : int = guiTime % 60;
   var fraction : int = (guiTime * 100) % 100;

   text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); 
   GUI.Label (Rect (400, 25, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already

}

Not tested, so I'm not sure if it works =P

more ▼

answered Mar 08 at 02:15 AM

Gindri gravatar image

Gindri
0

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

x552

asked: Feb 13, 2010 at 07:00 PM

Seen: 53510 times

Last Updated: Mar 08 at 02:15 AM