- Home /
Overwritting score with GUI
I have the following code with which I am showing the current score.
void OnGUI()
{
GUI.Label (new Rect (10, 40, 200, 100), "Level: " + ((int) Application.loadedLevel - 1 )+ '\n');
if (Application.loadedLevel == 2)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + "/ 18");
if (Application.loadedLevel == 3)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + "/ 25");
if (Application.loadedLevel == 4)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + "/ 39");
}
(one coin, one point) The problem is that, when a coin is collected the points are overwritting.
How can I fix this?
how? because I'm collecting a coins and the score is raising at the same time.
Answer by BlackWingsCorp · Jul 24, 2014 at 09:06 AM
Just as RedDevil said, in your script you're using the same variable for every level, so let's say the player finished the first level, once he starts the second the value of the coins will begin with that of the first level instead of 0. You can fix this in two ways: 1) For each level have a coin variable ex: coinLVL1, coinLVL2... 2) Once the next level starts return the coin variable to 0. You can then save the score of each in an array of ints or floats in order to display them. For example public int coins; public int nb = 0; public int coinScore[nb]; public bool isFinished = false;
voidUpdate{
if(isFinished)//add your conditions
nb++;
coinScore[nb] = coins;
coins = 0;
isFinished = false;
}
void OnGUI{
GUI.Label(new Rect (10, 60, 200, 100), "Score: " + coinScore[1] + "/ 18";
}
Hope this helps
I'd personally use the first method. But hey you never know when you need to use arrays right?
I'm using the second method already, but the problem is that at the beginning on screen I have 0/18 and when I collect the first coin the score is 1/18, but the "1" is written over the 0. The "0" is not deleted.
Your answer
Follow this Question
Related Questions
Displaying score on screen in specific place 1 Answer
Trying to get this coin system to work to no avail 1 Answer
Distribute terrain in zones 3 Answers
Adding score when enemy dies (Errors) 2 Answers
Highscore GUI not working properly help 0 Answers