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 Hyl1an231 · Apr 08, 2014 at 04:35 AM · score systemscoreboard

Adding different values to scoreboard

So I am working on a project where you click on certain cubes and they add up scores onto the scoreboard. There are a lot of different cubes in the scene. Some cubes are worth 5 points whereas some are worth 10, 50, 100, etc. I have the following code but does not seem to work the way I want it to as the score decreases when I click on a cube with a different value. Any help will be appreciated :)

 var score : int = 0;
 var pointsAdd : int = 5;
 var ScoreBoard : GUIText; // <- drag your GUIText here
 
 function OnMouseOver () 
 {
    if (Input.GetMouseButtonDown(0))
    {
         score += pointsAdd;
         ScoreBoard.text = ("Score: ") + score;
    }
 }
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
0

Answer by supernat · Apr 08, 2014 at 04:44 AM

Since you are assigning this script to each of your cubes, the variable score is unique for every cube, so every cube has a variable called score that starts out at 0. When you click one cube 5 times in a row, that cube's score variable is incremented properly and the GUIText you are referencing gets set to that score. Then you click another cube once, and the score variable on that cube gets incremented (from 0), and the GUIText is set to that score (making it appear that it has gone down).

The simple fix (correct? I couldn't say, as it depends on your needs) is to make score a static variable (Sorry I don't know how to do that in JS, but you probably can figure it out easy). As a static variable, score is now common, only one exists in the world, and all cubes share it instead of having their own scores. The rest of the code then just works.

Comment
Add comment · Show 2 · 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 Hyl1an231 · Apr 08, 2014 at 05:35 AM 0
Share

Thanks for your ideas. I shall try my best

avatar image Hyl1an231 · Apr 08, 2014 at 10:28 AM 0
Share

Okay, I now have it working. You were correct about the static variable. All I had to do was add the word 'static' in front of my score variable. Works like a charm:

 static var score : int = 0;
 var pointsAdd : int = 5;
 var ScoreBoard : GUIText; // <- drag your GUIText here
 
 function On$$anonymous$$ouseOver () 
 {
    if (Input.Get$$anonymous$$ouseButtonDown(0))
    {
         score += pointsAdd;
         ScoreBoard.text = ("Score: ") + score;
    }
 }

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

22 People are following this question.

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

Related Questions

Ive been trying to get a score counter to work for 2 weeks please help. 0 Answers

Saving Just Five Top Scores? 1 Answer

I need my script to load and save my players score to a text file . When I attach the script to a gameobject the script either is not found or it just prints 5,4,3,2,1 or all )0's. 0 Answers

Making a scoreboard with a timer 2 Answers

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 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