- Home /
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.
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.
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")