x


how to [Start a Timer] & Display as GUI

i have a simple trigger, Cube/box collider, named Startpoint.

when my player object crosses it, i want a timer to start counting up.
like, before the trigger, the GUI should be displaying [0:00:00] (min, secs, millisecs).
and OnTrigger, start the timer, 1,2,3,4,5,... seconds.

i also have 2nd trigger cube/box collider, named Endpoint.

when the player object crosses/triggers it, the timer stops, and that will be the player's ellapsed time.


here's what i've tried writing, referencing from other questions/answers and researching on my own, but it's not working =\

attached this c# script to my Startpoint game object:

using UnityEngine;
using System.Collections;

public class StartingLine : MonoBehaviour {

    private float startTime;
    private string ellapsedTime;

    void Awake(){

        startTime = Time.time;

    }

    // Update is called once per frame
    void Update () {

    }

    //void OnTriggerEnter(){
    //  GUI.Label(new Rect(10, 10, 100, 20), (startTime));
    //  float startTime = Time.time;
    //  
    //  guiText.text = ""+startTime;
    //}

    void OnGUI(){

        ellapsedTime = Time.time - startTime;

        GUI.Label(new Rect(10, 10, 100, 20), ellapsedTime);
    }
}

this script currently gives me the error mentioned above at the top of my question. can someone show me the right way of writing my timer? as well as the End time part.. and would i be able to create a gui skin, for a custom HUD that displays this timer?

more ▼

asked Jan 31 '11 at 05:29 PM

lvlup gravatar image

lvlup
21 7 7 15

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

1 answer: sort voted first

Working script:

using UnityEngine;
using System.Collections;

public class Finishline: MonoBehaviour 
{
  private float startTime;
  private float elapsedTime;

  void Awake(){
    startTime = 0;      
  }

  void Update () {

    if (startTime > 0)
    {
       elapsedTime = Time.time - startTime;
    }
  }

  void OnTriggerEnter(){
    startTime = Time.time;
  }

  void OnTriggerExit(){
    startTime = 0;
  }

  void OnGUI(){
    GUI.Label(new Rect(300, 100, 100, 20), (elapsedTime.ToString()));
  }
}

Now if you'd rather have one trigger start the timer, and entering a second would stop it, you'd OMIT the OnTriggerExit in this script, make startTime public, and have a new script that is simply:

class TimerStopper : MonoBehaviour
{
   GameObject finishGO;

   void Start() {
     finishGO = GameObject.Find("FinnishLine");
   }

   void OnTriggerEnter() {
    finishGO.GetComponent(typeOf(FinnishLine)).startTime = 0;
  }
more ▼

answered Jan 31 '11 at 05:34 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

that helped the float/string error.

but i havn't gotten any answer yet relating to the main question... =|

Feb 02 '11 at 07:13 AM lvlup

wish i could delete your answer so i can add a bounty to this question.. :\

Feb 02 '11 at 07:22 AM lvlup

I think you need to accumulate elapsed time in Update(). You might use Time.deltaTime

Feb 02 '11 at 07:25 AM DaveA

We'll get it. Do you want the timer to run while the person is in the trigger/collider? If so, use OnColliderStay. If it starts with one trigger and ends with a different one, that's a little different, not hard though.

Feb 02 '11 at 07:43 AM DaveA

Also, yes, you can update the GUI with the current running count, as you have it I think. You just need to accumulate time in Update or OnCollisionrStay

Feb 02 '11 at 07:44 AM DaveA
(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:

x3697
x352
x156
x146

asked: Jan 31 '11 at 05:29 PM

Seen: 5164 times

Last Updated: Feb 08 '11 at 01:05 AM