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
0
Question by Persona · Nov 02, 2010 at 08:00 PM · guitimerdisplay

Timer Display Question

static var timeRemaining : float = 60; var prefix = "Time Left: "; var suffix = " ";

function Awake(){ timeRemaining = 60;

}

function Update() { guiText.text = prefix + timeRemaining + suffix;

timeRemaining -= Time.deltaTime;

if(timeRemaining <= 0) { Application.LoadLevel ("Game Over"); } transform.position.x = 0.65; transform.position.y = 0.05; }

I have a timer but it displays the numbers after the decimals while I only want it to display whole numbers. Any suggestions?

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

5 Replies

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

Answer by skovacs1 · Nov 02, 2010 at 08:08 PM

If you just want the ceiling:

guiText.text = prefix + Mathf.Ceil(timeRemaining) + suffix;

If you just want to drop the decimal and get the floor:

guiText.text = prefix + Mathf.Floor(timeRemaining) + suffix;
Comment
Add comment · Show 1 · 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 Persona · Nov 02, 2010 at 08:54 PM 0
Share

Simple and effective. I'll take it!

avatar image
4

Answer by Peter G · Nov 02, 2010 at 08:22 PM

If you want to truncate the time, you could have Unity do the timing for you and then just print that.

var time: int = 60;

function Awake () { //Create the timer. //Method: InvokeRepeating("MethodName", delayFor1stFire : float, repeatTime: float); InvokeRepeating("FireTimer", 1, 1);

 //I could not figure out why you were calling these every frame.
 transform.position.x = 0.65;
 transform.position.y = 0.05;

}

function FireTimer() { time--;

 guiText.text = prefix + time.ToString() + suffix;

 if(timeRemaining &lt;= 0)
 {
      Application.LoadLevel ("Game Over");
 }

}

This will improve performance as well because you are not calling the method every frame. Only once per second. So, if performance matters, this is the fastest method.

Comment
Add comment · Show 1 · 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 Atnas1010 · Nov 02, 2010 at 08:45 PM 0
Share

Oooh, very nice :)

avatar image
1

Answer by Atnas1010 · Nov 02, 2010 at 08:02 PM

guiText.text = prefix + timeRemaining.ToString("f0") + suffix;

This should work.

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
avatar image
1

Answer by equalsequals · Nov 02, 2010 at 08:03 PM

You could try Mathf.Round() or something similar, or just convert the float to an int it will drop the fraction.

var intTimeRemaining : int = timeRemaining;
guiText.text = prefix + intTimeRemaining + suffix;

Unity JS has dynamic typecasting, so it should just work. I didn't test it, though.

Cheers

==

Comment
Add comment · Show 4 · 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 Atnas1010 · Nov 02, 2010 at 08:06 PM 0
Share

Converting the float to an int would make the counting stop, since Time.deltaTime is a float

avatar image skovacs1 · Nov 02, 2010 at 08:10 PM 0
Share

I believe equalsequals was referring to converting to an int inside the guiText.text, not at your declaration - this would not stop the counting.

avatar image Atnas1010 · Nov 02, 2010 at 08:13 PM 0
Share

How would one do that?

avatar image equalsequals · Nov 02, 2010 at 08:23 PM 0
Share

edited my post.

avatar image
0

Answer by Eric5h5 · Nov 02, 2010 at 10:10 PM

You can use an integer and subtract one every second, instead of using a float in Update. See my answer here: http://answers.unity3d.com/questions/12992/count-down-timer-per-second

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 Peter G · Nov 02, 2010 at 10:27 PM 0
Share

I believe that is what I did. ;)

avatar image Eric5h5 · Nov 02, 2010 at 10:46 PM 1
Share

@Peter G: Well, so it is...maybe I should read the other answers more thoroughly next time.... $$anonymous$$y the accepted answers are the less efficient ones. ;)

avatar image Persona · Nov 03, 2010 at 02:12 AM 0
Share

Hey, I go for simple enough for me to understand. Besides, I openly admit, I'm too incompetent right now to know the difference in the CPU cycle.

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

No one has followed this question yet.

Related Questions

how to [Start a Timer] & Display as GUI 1 Answer

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