- Home /
One highscore player pref setting highscores for all levels
Hi everyone, I seem to be having an awful lot of trouble with player prefs the last few days. I have similar code to this in each of my levels for when the level ends:
playerScript in my first level
if(healthSlider.value
if (PlayerPrefs.GetInt("forestRushHighScore", GUICounters.highestScore) < GUICounters.score)
{
GUICounters.highestScore = GUICounters.score;
PlayerPrefs.SetInt("forestRushHighScore", GUICounters.highestScore);
}
}
I have 8 levels, each one has a different key corresponding to that levels highscore. I also have a button on my main menu that I want to display the high scores for each level. The problem is, if I play one level, beat the high score and then check my highscores from the main menu, each level has the same high score as the level I just beat. I don't know why, I'm using each unique key.
my High Score script attached to the camera
void Update () { forestRush.text = "" + PlayerPrefs.GetInt("forestRushHighScore", GUICounters.highestScore); catchEm.text = "" + PlayerPrefs.GetInt("catchEmHighScore", GUICounters.highestScore); colorCoded.text = "" + PlayerPrefs.GetInt("colorCodedHighScore", GUICounters.highestScore); defence.text = "" + PlayerPrefs.GetInt("defenceHighScore", GUICounters.highestScore); target.text = "" + PlayerPrefs.GetInt("targetHighScore", GUICounters.highestScore); flight.text = "" + PlayerPrefs.GetInt("flightHighScore", GUICounters.highestScore); maze.text = "" + PlayerPrefs.GetInt("mazeHighScore", GUICounters.highestScore); jumpRunner.text = "" + PlayerPrefs.GetInt("jumpRunnerHighScore", GUICounters.highestScore); }
If anyone could point out something I'm doing wrong, i'd appreciate it, it's the final thing I have to do before I'm finished my game.
Answer by JayFitz91 · Sep 21, 2014 at 12:15 PM
I think I found the issue, in my high score script, I use the unique key but with the same GUICounters.highscore varible so I don't know if maybe somewhere, its getting overwritten. Anyway I gave each level an individual high score key and now it seems to work fine. The new code:
playerScript in my first level
if(healthSlider.value <= 0)
{
if (PlayerPrefs.GetInt("forestRushHighScore", GUICounters.forestRushHighScore) < GUICounters.score)
{
GUICounters.forestRushHighScore = GUICounters.score;
PlayerPrefs.SetInt("forestRushHighScore", GUICounters.forestRushHighScore);
PlayerPrefs.SetInt("forestRushScore", GUICounters.score);
}
GUICounters.ammo = 50;
GUICounters.score = 0;
Application.LoadLevel("endGame");
}
my High Score script attached to the camera
void Update ()
{
forestRush.text = "" + PlayerPrefs.GetInt("forestRushHighScore", GUICounters.forestRushHighScore);
catchEm.text = "" + PlayerPrefs.GetInt("catchEmHighScore", GUICounters.catchEmHighScore);
colorCoded.text = "" + PlayerPrefs.GetInt("colorCodedHighScore", GUICounters.colorCodedHighScore);
defence.text = "" + PlayerPrefs.GetInt("defenceHighScore", GUICounters.defenceHighScore);
target.text = "" + PlayerPrefs.GetInt("targetHighScore", GUICounters.targetHighScore);
flight.text = "" + PlayerPrefs.GetInt("flightHighScore", GUICounters.flightHighScore);
maze.text = "" + PlayerPrefs.GetInt("mazeHighScore", GUICounters.mazeHighScore);
jumpRunner.text = "" + PlayerPrefs.GetInt("jumpRunnerHighScore", GUICounters.jumpRunnerHighScore);
}
Your answer
Follow this Question
Related Questions
PlayerPrefs save after quit? 2 Answers
PlayerPrefs Highscore problem 1 Answer
How do I get my endless runner high score,How do I make high score for an endless runner 1 Answer
PlayerPrefs highscore system? 1 Answer
Implementing HighScore board 0 Answers