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?
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.