• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Jason Hamilton · Jun 27, 2010 at 12:11 PM · playerprefsdatabaseonlinehighscoresoffline

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
...
...
...
...
...
...
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikezNesh · Jun 27, 2010 at 01:11 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

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

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tetrad · Jun 27, 2010 at 05:51 PM 1
Share

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.

avatar image MikezNesh · Jun 28, 2010 at 12:48 PM 0
Share

WIll this score work for iPhone too?

avatar image spinaljack · Jun 28, 2010 at 08:36 PM 0
Share

yup, it'll work no problem

avatar image Jason Hamilton · Jun 30, 2010 at 01:23 AM 0
Share

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

avatar image MikezNesh · Jun 30, 2010 at 05:56 AM 0
Share

How would I set up a GUI table?

Show more comments
avatar image
1

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 :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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



Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kennnnny · Nov 13, 2012 at 06:34 PM 0
Share

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?

avatar image flamy · Nov 13, 2012 at 06:38 PM 0
Share

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

how many times you are saving the score???

avatar image Kennnnny · Nov 13, 2012 at 06:49 PM 0
Share

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

avatar image Kennnnny · Nov 13, 2012 at 07:29 PM 0
Share

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.

avatar image cai_bisabaik · May 08, 2013 at 09:43 AM 1
Share

where does eventmanager coming from anyway?

Show more comments
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SomeRandomGuy · Oct 27, 2012 at 02:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to save and retreive high score.... 4 Answers

Save firebase database in offline mode too with the help of Google Play Games Services,Save firebase database offline with Google Play Games Services 0 Answers

it is Playerprefs can use in saving highscore and both PC having this can same see data in Highscores? 0 Answers

High Score with player prefs confusion (JAVA) 0 Answers

how to make high scores using playerprefs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges