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 Dreamblur · Nov 23, 2010 at 05:41 AM · guiscorelabel

Score display not working

Can anyone tell me what's wrong with my code?

static var nScore : int = 0;

function Update() { var nScoreHundredMillionsDigit : int = (nScore % 1000000000) / 100000000; var nScoreTenMillionsDigit : int = (nScore % 100000000) / 10000000; var nScoreMillionsDigit : int = (nScore % 10000000) / 1000000; var nScoreHundredThousandsDigit : int = (nScore % 1000000) / 100000; var nScoreTenThousandsDigit : int = (nScore % 100000) / 10000; var nScoreThousandsDigit : int = (nScore % 10000) / 1000; var nScoreHundredsDigit : int = (nScore % 1000) / 100; var nScoreTensDigit : int = (nScore % 100) / 10; var nScoreOnesDigit : int = nScore % 10; }

function OnGUI() { GUI.Label(Rect (Screen.width - 40, 20, 20, 20), GetComponent(Score).Update().nScoreOnesDigit.ToString()); GUI.Label(Rect (Screen.width - 60, 20, 20, 20), GetComponent(Score).Update().nScoreTensDigit.ToString()); GUI.Label(Rect (Screen.width - 80, 20, 20, 20), GetComponent(Score).Update().nScoreHundredsDigit.ToString()); GUI.Label(Rect (Screen.width - 100, 20, 20, 20), GetComponent(Score).Update().nScoreThousandsDigit.ToString()); GUI.Label(Rect (Screen.width - 120, 20, 20, 20), GetComponent(Score).Update().nScoreTenThousandsDigit.ToString()); GUI.Label(Rect (Screen.width - 140, 20, 20, 20), GetComponent(Score).Update().nScoreHundredThousandsDigit.ToString()); GUI.Label(Rect (Screen.width - 160, 20, 20, 20), GetComponent(Score).Update().nScoreMillionsDigit.ToString()); GUI.Label(Rect (Screen.width - 180, 20, 20, 20), GetComponent(Score).Update().nScoreTenMillionsDigit.ToString()); GUI.Label(Rect (Screen.width - 200, 20, 20, 20), GetComponent(Score).Update().nScoreHundredMillionsDigit.ToString()); }

What I want is for the score to be displayed as 000000000 when the score is 0.

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 duck · Nov 23, 2010 at 07:02 AM

Your problem is that you are declaring the variables inside the update function (at the same time as assigning their value). This means their "scope" is within that function only, and when they fall out of scope (i.e. when the function ends) they are forgotten.

To solve this, declare the variables outside the function, at the top of the script (like your nScore value), and just assign their values inside the update function.

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 denewbie · Nov 23, 2010 at 03:06 PM 0
Share

With respect to: I would guess that the reason Dreamblur is breaking it into individual digits is so that he/she can display customised graphics for each digit. Duck 4 hours ago

Oh i see.. I guess i'll just tag my part with yours just in case its helpfull...

Try this: nScore.ToString("D9")

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 can i make a local high score system with C#? 3 Answers

How would you code a gui to select a label? 0 Answers

coin collecting with onscreen score 1 Answer

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

GUI.Label positioning for many device resolutions 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