- Home /
How do I make a highscores board?
I just want a simple highscores board, that will appear when the game is over... e.g.
1. Player name 99999
2. my name 88888
...
...
...
...
...
...
I'm interested in this too. But I am more for a LOCAL scoreboard if anyone wants to tell how to do that too.
Answer by spinaljack · Jun 27, 2010 at 03:59 PM
Server based highscores:
http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores
Pretty straightforward.
@MikezNesh
For a local highscore save data on PlayerPrefs (if it's not full) Do this:
function AddScore(name : String, score : int){
var newScore : int;
var newName : String;
var oldScore : int;
var oldName : String;
newScore = score;
newName = name;
for(i=0;i<10;i++){
if(PlayerPrefs.HasKey(i+"HScore")){
if(PlayerPrefs.GetInt(i+"HScore")<newScore){
// new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i+"HScore");
oldName = PlayerPrefs.GetString(i+"HScoreName");
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = oldScore;
newName = oldName;
}
}else{
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = 0;
newName = "";
}
}
}
To display the high score set up a GUI to display the text and int in a table and store the information from player prefs in variables. Initialise the high score table by calling AddScore at the start of your game.
var 1stScore : int; var 1stName : String; var 2ndScore : int; var 2ndName : String; etc...
function UpdateHScore(){ 1stScore = PlayerPrefs.GetInt(0HScore); 1stName = PlayerPrefs.GetString(0HScoreName); etc.... }
Alternatively use PlayerPrefsX from the wiki:
http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs
One trick I use for storing a bunch of stuff like that in playerprefs is just to use JSON. That way I can just serialize a hash table to a string and store it that way.
I still can't get my head around this... is there a tutorial or something out there on the inter webs?
Answer by tokenshi · Jul 29, 2011 at 06:28 PM
So in using this on an android game, it'll store the players name just fine, but it won't store the score - it simply puts a 0 in there :(
Answer by flamy · May 14, 2012 at 07:10 AM
Hi this is a very simple high score system for saving a int score and a name. Hope the code is clear
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// High score manager.
/// Local highScore manager for LeaderboardLength number of entries
///
/// this is a singleton class. to access these functions, use HighScoreManager._instance object.
/// eg: HighScoreManager._instance.SaveHighScore("meh",1232);
/// No need to attach this to any game object, thought it would create errors attaching.
/// </summary>
public class HighScoreManager : MonoBehaviour
{
private static HighScoreManager m_instance;
private const int LeaderboardLength = 10;
public static HighScoreManager _instance {
get {
if (m_instance == null) {
m_instance = new GameObject ("HighScoreManager").AddComponent<HighScoreManager> ();
}
return m_instance;
}
}
void Awake ()
{
if (m_instance == null) {
m_instance = this;
} else if (m_instance != this)
Destroy (gameObject);
DontDestroyOnLoad (gameObject);
}
public void SaveHighScore (string name, int score)
{
List<Scores> HighScores = new List<Scores> ();
int i = 1;
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
if (HighScores.Count == 0) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
} else {
for (i=1; i<=HighScores.Count && i<=LeaderboardLength; i++) {
if (score > HighScores [i - 1].score) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Insert (i - 1, _temp);
break;
}
if (i == HighScores.Count && i < LeaderboardLength) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
break;
}
}
}
i = 1;
while (i<=LeaderboardLength && i<=HighScores.Count) {
PlayerPrefs.SetString ("HighScore" + i + "name", HighScores [i - 1].name);
PlayerPrefs.SetInt ("HighScore" + i + "score", HighScores [i - 1].score);
i++;
}
}
public List<Scores> GetHighScore ()
{
List<Scores> HighScores = new List<Scores> ();
int i = 1;
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
return HighScores;
}
public void ClearLeaderBoard ()
{
//for(int i=0;i<HighScores.
List<Scores> HighScores = GetHighScore();
for(int i=1;i<=HighScores.Count;i++)
{
PlayerPrefs.DeleteKey("HighScore" + i + "name");
PlayerPrefs.DeleteKey("HighScore" + i + "score");
}
}
void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}
public class Scores
{
public int score;
public string name;
}
to check if this work check the following script, just add this to a game object and you will be able to check its functionality
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MenuController : MonoBehaviour {
string name="";
string score="";
List<Scores> highscore;
// Use this for initialization
void Start () {
EventManager._instance._buttonClick += ButtonClicked;
highscore = new List<Scores>();
}
void ButtonClicked(GameObject _obj)
{
print("Clicked button:"+_obj.name);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Name :");
name = GUILayout.TextField(name);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Score :");
score = GUILayout.TextField(score);
GUILayout.EndHorizontal();
if(GUILayout.Button("Add Score"))
{
HighScoreManager._instance.SaveHighScore(name,System.Int32.Parse(score));
highscore = HighScoreManager._instance.GetHighScore();
}
if(GUILayout.Button("Get LeaderBoard"))
{
highscore = HighScoreManager._instance.GetHighScore();
}
if(GUILayout.Button("Clear Leaderboard"))
{
HighScoreManager._instance.ClearLeaderBoard();
}
GUILayout.Space(60);
GUILayout.BeginHorizontal();
GUILayout.Label("Name",GUILayout.Width(Screen.width/2));
GUILayout.Label("Scores",GUILayout.Width(Screen.width/2));
GUILayout.EndHorizontal();
GUILayout.Space(25);
foreach(Scores _score in highscore)
{
GUILayout.BeginHorizontal();
GUILayout.Label(_score.name,GUILayout.Width(Screen.width/2));
GUILayout.Label(""+_score.score,GUILayout.Width(Screen.width/2));
GUILayout.EndHorizontal();
}
}
}
This script is working fine on pc but when used on an android device it only saves (or shows) 1 score, the first score.
What might be the problem here?
it works fine for me in both the platforms...
how many times you are saving the score???
I put a restriction of 5 entries for my highscore. It doesn't work anymore when I try to save a 2nd score so no I don't think the problem is that I'm saving 100s of scores
Ok after some more testing it seems that if I try to add a new score that's lower than the scores that are already in the list and the list isn't full yet, it doesn't get added.
where does eventmanager co$$anonymous$$g from anyway?
Answer by Danboe2009 · Aug 01, 2012 at 12:32 AM
I used the code above and I feel like it goes in to a loop uncontrolled. Here is an example of what happens. If the player gets a new score that is higher then the highest score then it wipes out the old score and replaces it with the highest.
So: Dan : 75 Dan : 50 Dan : 25 Dan : 25 Dan : 25
Becomes: Dan : 100 Dan : 100 Dan : 100 Dan : 100 Dan : 100
are you sure you haven't forgotten the newscore = oldscore; part in the loop? it makes sure the next time the loop is called it will be using the score that got overthrown by the new highscore, by putting it in the newscore variable
Your answer
Follow this Question
Related Questions
how to save and retreive high score.... 4 Answers
Player Prefs question. 1 Answer
Adding extra information to Server Side Highscores 4 Answers