How to save highscore in Unity

How To Tech
Jul 30, 2021

Here’s the original post.

The simplest way to save highscore in Unity is to use the built-in PlayerPrefs.

Usually is better to place the code in your game over method.

Before saving the score, you have to check if the key exist and then check if the new score is higher than the highscore. If the key doesn’t exist you have to create it of course.

Here below you can have an example of how to achieve this.

if(PlayerPrefs.HasKey("hiScore")
{
if(newScore > Playerprefs.GetInt("hiScore"))
{
highScore = newScore;
PlayerPrefs.SetInt("hiScore", highScore);
PlayerPrefs.Save();
}
}
else
{
if(newScore > highScore)
{
highScore = newScore;
PlayerPrefs.SetInt("hiScore", highScore);
PlayerPrefs.Save();
}
}

--

--

How To Tech

Quick tutorials on Photoshop, Unity 3D, Facebook, WhatsApp, Windows and some Game news