Leader boards are a pretty common thing in games. They’re actually pretty simple to implement once you get your head around it.

I found a script online that organizes new high-scores with both a score and a name. It checks the new score from the lowest highscore in the list (in my case 5). If it’s higher then it moves the old highscore down one, and writes the new one in it’s place. It then keeps doing that further up the list until it is either it is smaller than the next highscore, or it reaches first place.

You start with a basic organizing script that will shuffle new scores to the correct position. I used PlayerPrefs (a unity thing) to keep the scores permanently so they can be called even after closing and reopening the game.

void LeaderboardPlacement (int currentScore) {

	// leaderboard for 5 places
	for (int i = 5; i >= 1; i--) {
		// adds the position number to the end of highscore
		string highScore = "highscore" + i.ToString ();
 		
		// if current score is higher than particular highscore
		if (currentScore > PlayerPrefs.GetInt (highScore)) {

			// creates a temporary position to shuffle score down
			int j = i+1;

			// saves the currently placed highscore
			int oldScore = PlayerPrefs.GetInt (highScore);

			// finds the next lowest highscore i.e. 4 -> 5
			string prevHighScore = "highscore" + j.ToString ();

			// moves the old highscore to the lower position
			PlayerPrefs.SetInt (prevHighScore, oldScore);

			// sets the new highscore its current position
			PlayerPrefs.SetInt (highScore, currentScore);
		}
	}
}

So now that we have that sorted, all we have to do is add in the names!

void LeaderboardPlacement (int currentScore, string currentName) {

	// leaderboard for 5 places
	for (int i = 5; i >= 1; i--) {
		// adds the position number to the end of highscore
		string highScore = "highscore" + i.ToString ();
		string userName = "username" + i.ToString ();
 		
		// if current score is higher than particular highscore
		if (currentScore > PlayerPrefs.GetInt (highScore)) {

			// creates a temporary position to shuffle score down
			int j = i+1;

			// saves the currently placed highscore
			int oldScore = PlayerPrefs.GetInt (highScore);

			// finds the next lowest highscore i.e. 4 -> 5
			string prevHighScore = "highscore" + j.ToString ();

			// moves the old highscore to the lower position
			PlayerPrefs.SetInt (prevHighScore, oldScore);

			// saves the currently placed username
			int oldName = PlayerPrefs.GetInt (userName);

			// finds the next lowest highscore i.e. 4 -> 5
			string prevUserName = "username" + j.ToString ();

			// moves the old username to the lower position
			PlayerPrefs.SetInt (prevUserName, oldName);

			// sets the new username its current position
			PlayerPrefs.SetInt (userName, currentName);
		}
	}
}

And there you have it! A working leaderboard! And all you have to do to get more or less is change the 5 in the first if () to however many you want.
You can then call each value by doing something like

int highscoreThree = PlayerPrefs.GetInt ("highscore3");
Advertisements