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();
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

How To Tech
How To Tech

Written by How To Tech

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

Responses (1)

Write a response