PlayerPrefs Not working. How Can I fix it?
I make a game like Zigzag. All thing goes well just High score not counting. I use PlayerPrefs for saving High Score. But its not working. Here is my project file link. How can I fix it?
Answer by KoenigX3 · Jan 16, 2017 at 10:23 PM
There is a stopScore() function in your ScoreManager script which looks like this:
public void stopScore()
{
CancelInvoke("incrementScore");
PlayerPrefs.SetInt("score", score);
if (PlayerPrefs.HasKey("highScore"))
{
if (score > PlayerPrefs.GetInt("highScore"))
{
PlayerPrefs.SetInt("highScore", score);
}
else
{
PlayerPrefs.SetInt("highScore", score);
}
}
}
As you can see, you are checking whether the current score is bigger than the high score, but since the 'else' statement contains the same script as the 'if' statement, it will always rewrite the high score with the current score. Also, if the player has no previous high score, it will not write a new one, which is also a problem.
Change it to this:
public void stopScore()
{
CancelInvoke("incrementScore");
PlayerPrefs.SetInt("score", score);
if (PlayerPrefs.HasKey("highScore"))
{
if (score > PlayerPrefs.GetInt("highScore"))
{
PlayerPrefs.SetInt("highScore", score);
}
}
else
{
PlayerPrefs.SetInt("highScore", score);
}
}
Now it will check if the player has a previous high score - if so, it will compare them, and write the bigger score into high score. If the player has no previous high score, it will use the current score.
Finally its working. Thanks. A chilly mistake give me so much pain.
Your answer
Follow this Question
Related Questions
Highscore Not Updating Upon Death 1 Answer
how do i store highscore locally C# (SIMPLE) 3 Answers
Highscore not working 0 Answers
Please help how to save last index and load 1 Answer
Display PlayerPref in InputField 1 Answer