Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
-1
Question by zero84085 · Jul 27, 2012 at 01:46 AM · guitimertime.deltatimegui text

Timer and gui text

hi guys this is my simple timer and is working perfect, but how i can display the countdown of the timer in screen with a gui text i get a error when i try and i must do something with string...

var timer : float = 10;

function Update(){

timer -= Time.deltaTime;

}

so we have the variable timer and is float and equal with 10 and timer get subtracted with Time.deltaTime every frame so that it but how i can display the minutes/seconds as gui text?

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

2 Replies

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

Answer by aldonaletto · Jul 27, 2012 at 02:39 AM

You must create a GUIText and set a reference to it in your script, which will become something like this:

 var gText: GUIText; // drag here the GUIText from Hierarchy view
 var timer: float = 10;
 
 function Update(){
   timer -= Time.deltaTime;
   if (timer < 0) timer = 0; // clamp the timer to zero
   var seconds: int = timer % 60; // calculate the seconds
   var minutes: int = timer / 60; // calculate the minutes
   gText.text = minutes + ":" + seconds;
 }

This formatting doesn't work fine with negative numbers, thus the timer is clamped to zero. If you need to display negative values, change the function Update to this:

 ...
 function Update(){
   timer -= Time.deltaTime;
   var t = Mathf.Abs(timer); // get the absolute timer value
   var seconds: int = t % 60; // calculate the seconds
   var minutes: int = t / 60; // calculate the minutes
   var minSec = minutes + ":" + seconds; // create the formatted string
   if (timer < 0) minSec = "-" + minSec; // add a minus sign if negative
   gText.text = minSec; // update the GUIText
 }

EDITED:

String concatenation isn't a good idea because it generates lots of intermediate memory allocations, which may cause performance hiccups when garbage collection (free memory recuperation) takes place. A better solution is to use .NET/Mono String.Format:

   gText.text = String.Format("{0:00}:{1:00}", minutes, seconds);

Each number is defined in the formatting string inside curly brackets, where the digit before the colon tells which argument to use (starting at 0), and the digits after the colon show the format. Take a look at .NET docs for more details.

Comment
Add comment · Show 3 · 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 zero84085 · Jul 27, 2012 at 09:20 AM 1
Share

i find very nice tips this and i understand the code 100% nice job :D

avatar image DK Reigns · Dec 01, 2014 at 06:54 PM 0
Share

How can I display milliseconds ? :)

avatar image aldonaletto · Dec 06, 2014 at 10:42 PM 0
Share

@D$$anonymous$$ Reigns, you can separate the fractional part with % 1 (modulo 1):

 var mSecs: float = t % 1;

and format the number with 3 decimal places:

 gText.text = mSecs.ToString("F3");

But if you want the milliseconds as an integer, multiply the result by 1000 and format it with no decimal places:

 var mSecs: float = (t % 1)*1000;
 gText.text = mSecs.ToString("F0");
avatar image
1

Answer by AlucardJay · Jul 27, 2012 at 01:54 AM

This topic has been discussed extensively. A search engine is your friend ...

http://lmgtfy.com/?q=unity+gui+timer

http://answers.unity3d.com/questions/11458/How-to-make-a-timer-in-the-game.html

http://answers.unity3d.com/questions/45455/how-to-start-a-timer-display-as-gui.html

http://www.youtube.com/watch?v=aHf96fBBYM0&feature=player_embedded

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer progress bar 0 Answers

Countdown Timer 1 Answer

Problem with Footstep System 1 Answer

GUI elements vanish when publishing 1 Answer

How Would I Make A GUI Label Fade After A Certain Amount Of Time? 1 Answer


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