x


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
...
...
...
...
...
...
more ▼

asked Jun 27 '10 at 12:11 PM

Jason Hamilton gravatar image

Jason Hamilton
445 68 73 80

I'm interested in this too. But I am more for a LOCAL scoreboard if anyone wants to tell how to do that too.

Jun 27 '10 at 01:11 PM MikezNesh
(comments are locked)
10|3000 characters needed characters left

6 answers: sort newest

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

more ▼

answered Jun 27 '10 at 03:59 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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.

Jun 27 '10 at 05:51 PM Tetrad

WIll this score work for iPhone too?

Jun 28 '10 at 12:48 PM MikezNesh

yup, it'll work no problem

Jun 28 '10 at 08:36 PM spinaljack

I still can't get my head around this... is there a tutorial or something out there on the inter webs?

Jun 30 '10 at 01:23 AM Jason Hamilton

How would I set up a GUI table?

Jun 30 '10 at 05:56 AM MikezNesh
(comments are locked)
10|3000 characters needed characters left

How could I revert it to? name01 85s name02 101s name03 150s . . . I mean from the lowest value to the highest?

more ▼

answered Sep 26 '12 at 10:33 AM

mydraff gravatar image

mydraff
1

(comments are locked)
10|3000 characters needed characters left

How could I revert it to? name01 85s name02 101s name03 150s . . . I mean from the lowest value to the highest?

more ▼

answered Sep 26 '12 at 10:23 AM

mydraff gravatar image

mydraff
1

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 01 '12 at 12:32 AM

Danboe2009 gravatar image

Danboe2009
3 3

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

Oct 27 '12 at 02:49 PM SomeRandomGuy
(comments are locked)
10|3000 characters needed characters left

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

answered May 14 '12 at 07:10 AM

flamy gravatar image

flamy
3.5k 5 11 37

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?

Nov 13 '12 at 06:34 PM Kennnnny

it works fine for me in both the platforms...

how many times you are saving the score???

Nov 13 '12 at 06:38 PM flamy

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

Nov 13 '12 at 06:49 PM Kennnnny

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.

Nov 13 '12 at 07:29 PM Kennnnny

hi the bug is fixed now, sorry for late reply, i dont follow old posts =/

Nov 23 '12 at 01:12 PM flamy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x294
x232
x113
x42
x17

asked: Jun 27 '10 at 12:11 PM

Seen: 16814 times

Last Updated: May 08 at 09:43 AM