• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lvlup · Jan 31, 2011 at 05:29 PM · guitimerdisplaystart

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DaveA · Jan 31, 2011 at 05:34 PM

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; }

Comment
Add comment · Show 27 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lvlup · Feb 02, 2011 at 07:13 AM 0
Share

that helped the float/string error.

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

avatar image lvlup · Feb 02, 2011 at 07:22 AM 0
Share

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

avatar image DaveA · Feb 02, 2011 at 07:25 AM 0
Share

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

avatar image DaveA · Feb 02, 2011 at 07:43 AM 0
Share

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.

avatar image DaveA · Feb 02, 2011 at 07:44 AM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Timer Display Question 5 Answers

Lerpz Escapes question 1 Answer

hide gui.label after an event 1 Answer

Displaying PlayerPref As Text Using New Unity GUI 1 Answer

GUIUtility.DisplayCustomMenu().. how can I disable one of these menu items.. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges