How can I store highscores in PlayerPrefs?

Hey, Id like to do a local highscores for my game, so I intend to use PlayerPrefs. I have absolutely no idea where to start, I suppose how to enter your name at the end??? anyways, the size of thing Id be looking for is top 5 scores with name next to them, can someone help me with this? and explanation would be great and snippets of code (not the whole thing I want to learn it not copy! :D) would be much appreciated, thankyou.

Im using javascript or unity-script whatever its called. :)

If you know you will be using 5 players;

PlayerPrefs.GetString("RecordName0", String.Empty);
PlayerPrefs.GetInt("RecordScore0", -1);

PlayerPrefs.GetString("RecordName1", String.Empty);
PlayerPrefs.GetInt("RecordScore1", -1);

PlayerPrefs.GetString("RecordName2", String.Empty);
PlayerPrefs.GetInt("RecordScore2", -1);

PlayerPrefs.GetString("RecordName3", String.Empty);
PlayerPrefs.GetInt("RecordScore3", -1);

PlayerPrefs.GetString("RecordName4", String.Empty);
PlayerPrefs.GetInt("RecordScore4", -1);

The keys could be formatted `String.Format("RecordName{0}", recordIndex);` and `String.Format("RecordScore{0}", recordIndex);`.

Then you could provide methods similar to:

function GetScore(recordIndex : int) : int
    var key = String.Format("RecordScore{0}", recordIndex);
    return PlayerPrefs.GetInt(key, -1);

function SetScore(recordIndex : int, score : int)
    var key = String.Format("RecordScore{0}", recordIndex);
    PlayerPrefs.SetInt(key, score);

function GetName(recordIndex : int) : String
    var key = String.Format("RecordName{0}", recordIndex);
    return PlayerPrefs.GetString(key, String.Empty);

function SetName(recordIndex : int, name : String)
    var key = String.Format("RecordScore{0}", recordIndex);
    PlayerPrefs.SetString(key, name);

Then you can get the values like:

var topName = GetName(0);
var topScore = GetScore(0);

With some tinkering about I am sure you can make better, higher level methods like:

function SetRecord(score : int, name : String)
    // Puts score and name to highscore, pushing worse score off the top 5.
    // You wanted to figure out some stuff yourself :)
    // But a tip is that you'd use the previous Get/Set methods.

You asked exactly the same here ! Whats wrong with the answer you accepted ?

There is a super tutorial of a full RPG Burg Zerg Arcade, which teaches that part very well, but is in C #, there is only you adapt to JavaScript, the Internet has some tutorials on how to do it ... Good Luck!


  1. Unity3D Tutorial - PlayerPrefs 1 / x
  2. Unity3D Tutorial - PlayerPrefs 2 / x
  3. Unity3D Tutorial - PlayerPrefs 3 / x
  4. Unity3D Tutorial - PlayerPrefs 4 / x
  5. Unity3D Tutorial - PlayerPrefs 5 / x
  6. Unity3D Tutorial - PlayerPrefs 6 / x