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 :slight_smile:

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;
   }
}

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.