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 Will 7 · Sep 07, 2010 at 09:14 PM · javascriptguivariablesnaphorizontal-slider

Limiting digits in GUI displayed variables (or "snap to" GUI slider).

I have a metronome controlled by a GUI slider in my game. I have then displayed the value of the slider variable using GUI.Label. This works great, only the variable often has way to many decimals (i want it to have none). So obscure values come up for the value. Is there a way to preferably create a "snap to" GUI slider or just limit the no. of decimals to be displayed on the label? Any help will be greatly appreciated. Oh and I used variable.ToString() for displaying the variable in the GUI.

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
1
Best Answer

Answer by Mike 3 · Sep 07, 2010 at 09:22 PM

variable.ToString("F0");

will specify 0 decimal places or the string (F2 will be 2 decimal places, etc. in case you ever need to do that)

To answer your comment - if you want it to snap to every 5, in your specific case:

c#

variable = (((int)(variable - 3)) / 5) * 5;

js

var temp : int = (variable - 3);
variable = (temp / 5) * 5;

Should do it

Comment
Add comment · Show 7 · 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 Will 7 · Sep 07, 2010 at 09:27 PM 0
Share

Thankyou, i was wondering what the parentheses were for...

avatar image Will 7 · Sep 07, 2010 at 09:34 PM 0
Share

I just noticed that sometimes values such as 153 come up and then the next value down may be 148 (or alike). So 150 (for instance) as a metronome speed is unobtainable. Would there be anyway to as i mentioned create a "snap-to" GUI Slider. But this should be sufficient for now.

avatar image Mike 3 · Sep 07, 2010 at 10:21 PM 0
Share

Added some basic code for snapping to increments of 5

avatar image Will 7 · Sep 10, 2010 at 02:47 PM 0
Share

Thanks for the reply, but which variables do I swap the "variable" and "temp" with? Do I put it in "function Update () {}"?

avatar image Will 7 · Sep 10, 2010 at 02:48 PM 0
Share

All i can see it doing is removing 3 from "variable"?

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Change GUI.Label text while it's active? 1 Answer

Change GUI Value with Variable 1 Answer

Need help with GUI label not displaying variable 1 Answer

scale textures using scrollbars that show/enable when texture is clicked on 0 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