Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by bsmith · Feb 11, 2015 at 01:36 PM · c#leaderboardhighscoresscoreboard

Leaderboard Scores only posts highest score?

Hello, So I have made a fully functional Leaderboard System that will display the top 3 scores. The only problem is, is that only the highest score will display.

Example: Name Score Name Score Name Score

Instead of what it is supposed to look like: Name1 Score1 Name2 Score2 Name3 Score3

This is my current code and I am using an IEnumerator to display the leaderboard.

 IEnumerator UpdateScore(){
     highScore2 = highScore3;
     PlayerPrefs.SetString(highScore3TextKey, userName3);
     PlayerPrefs.SetInt(highScore3Key, highScore3);
     PlayerPrefs.Save();
     yield return new WaitForSeconds(1);
     highScore1 = highScore2;
     PlayerPrefs.SetString(highScore2TextKey, userName2);
     PlayerPrefs.SetInt(highScore2Key, highScore2);
     PlayerPrefs.Save();
     yield return new WaitForSeconds(1);
     userHighScore = highScore1;
     PlayerPrefs.SetString(highScore1TextKey, userName1);
     PlayerPrefs.SetInt(highScore1Key, highScore1);
     PlayerPrefs.Save();
     Start();
 }

What needs to happen is when there is a new highscore, highscore 3 becomes, highscore 2, highscore 2 becomes 1 and highscore1 becomes the new highscore. Thanks if you can help in advanced!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MaxEden · Feb 11, 2015 at 02:39 PM

Consider this highscore board snippet

  class HighscoreBoard
     {
         public class HighscoreRecord
         {
             public string Name;
             public int Score;
         }
 
         public List<HighscoreRecord> Records = new List<HighscoreRecord>();
 
         public void AddRecord(string name, int score)
         {
             Records.Add(new HighscoreRecord()
             {
                 Name = name,
                 Score = score
             });
 
 
             Records = Records
                 //Sort
                 .OrderByDescending(p => p.Score)
                 //Take first 3
                 .Take(3)
                 .ToList();
 
             string recordsAsString = String.Join(";",
                 Records
                 .Select(RecordToString)
                 .ToArray()
                 );
 
             PlayerPrefs.SetString("Highscores", recordsAsString);
         }
 
         public void ReadHighscores()
         {
             if (!PlayerPrefs.HasKey("Highscores")) return;
 
             Records = PlayerPrefs.GetString("Highscores")
                 .Split(';')
                 .Select(RecordFromString)
                 .ToList();
         }
 
         string RecordToString(HighscoreRecord record)
         {
             return record.Name + ':' + record.Score;
         }
 
         HighscoreRecord RecordFromString(string str)
         {
             string[] parts = str.Split(':');
             return new HighscoreRecord()
             {
                 Name = parts[0],
                 Score = Int32.Parse(parts[1])
             };
         }
     }

Of course there is a pinch of Linq magic, but it's small and handy.

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 Sabrino · Aug 28, 2015 at 09:54 PM 0
Share

This works really well, thank you. I had to make some small changes, otherwise it wouldn't compile:

 String.Join should be string.Join
 Int32.Parse should be int.Parse

and I had to declare the Select method this way:

 Select<HighscoreRecord, string>(RecordToString)
 Select<string, HighscoreRecord>(RecordFromString)

Otherwise, it works like a charm.

avatar image
0

Answer by Dicen777 · Feb 11, 2015 at 02:25 PM

You are essentially assigning highScore3 to all other scores.

EDIT: I think that you are under the assumption that the left gets assigned to the right, which is why the following looks correct. The left is always the updated value and the right is the value that it gets updated to.

 highScore2 = highScore3;
 highScore1 = highScore2;
 userHighScore = highScore1;

You want to reverse the variables in respect to "=". I brought all the assignments up to the top since it will still work and it is easier to understand the change IMO.

 IEnumerator UpdateScore(){

     highScore3 = highScore2;
     highScore2 = highScore1;
     highScore1 = userHighScore;

     PlayerPrefs.SetString(highScore3TextKey, userName3);
     PlayerPrefs.SetInt(highScore3Key, highScore3);
     PlayerPrefs.Save();
     yield return new WaitForSeconds(1);

     PlayerPrefs.SetString(highScore2TextKey, userName2);
     PlayerPrefs.SetInt(highScore2Key, highScore2);
     PlayerPrefs.Save();
     yield return new WaitForSeconds(1);

     PlayerPrefs.SetString(highScore1TextKey, userName1);
     PlayerPrefs.SetInt(highScore1Key, highScore1);
     PlayerPrefs.Save();
     Start();
 }
Comment
Add comment · Show 5 · 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 bsmith · Feb 11, 2015 at 05:33 PM 0
Share

Thanks! But still for some reason it still does the same thing. It will update all the names for the score as the same name but it will also just displays the score as 0 for each highscore..

avatar image Dicen777 · Feb 11, 2015 at 05:47 PM 0
Share

So this code only saves to a file to be accessed later. Can you post the code that retrieves this information and displays the score?

avatar image bsmith · Feb 11, 2015 at 06:04 PM 0
Share

I am saving everything to PlayerPrefs and I am display everything through each .text .

void Start () {

     userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0);

     Initalize();

     highScoreNameText1.text = userName1;
     highScoreNameText2.text = userName2;
     highScoreNameText3.text = userName3;
     highScoreText1.text = highScore1.ToString();
     highScoreText2.text = highScore2.ToString();
     highScoreText3.text = highScore3.ToString();
     userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0);

}

     void Initalize(){
         userName1 = PlayerPrefs.GetString(highScore1Text$$anonymous$$ey, "Empty");
         userName2 = PlayerPrefs.GetString(highScore2Text$$anonymous$$ey, "Empty");
         userName3 = PlayerPrefs.GetString(highScore3Text$$anonymous$$ey, "Empty");
         highScore1 = PlayerPrefs.GetInt(highScore1$$anonymous$$ey,0);
         highScore2 = PlayerPrefs.GetInt(highScore2$$anonymous$$ey,0);
         highScore3 = PlayerPrefs.GetInt(highScore3$$anonymous$$ey,0);
 }









avatar image Dicen777 · Feb 11, 2015 at 07:03 PM 0
Share

I think at this point you want to do some debugging in order to see where things are going wrong. You can use Debug.Log to do this.

For instance, start with putting a couple lines in the functions that are involved so you know they are getting called. so in UpdateScore() make the first line

 Debug.Log("UpdateScore Called");

once you know that all your functions are getting called correctly, start logging information from those calls like variable values.

 Debug.Log(highScore1.ToString());

and see where the value changes from the score to 0. If you can find where that is happening and still can't solve the issue, comment what you found.

One issue (presumably) that I see though is you are only updating the text in the Start() function, meaning it only gets updated when the attached object is created.

avatar image bsmith · Feb 12, 2015 at 02:45 AM 0
Share
 void Start () {

     highScoreNameText1.text = PlayerPrefs.GetString(highScore1Text$$anonymous$$ey, "Empty");
     highScoreNameText2.text = PlayerPrefs.GetString(highScore2Text$$anonymous$$ey, "Empty"); //displays and retives the highscore texts
     highScoreNameText3.text = PlayerPrefs.GetString(highScore3Text$$anonymous$$ey, "Empty");
     highScoreText1.text = updateHighScore1.ToString();
     highScoreText2.text = updateHighScore2.ToString(); //displays the highscore numbers
     highScoreText3.text = updateHighScore3.ToString();

     updateHighScore1 = PlayerPrefs.GetInt(highScore1$$anonymous$$ey,0);
     updateHighScore2 = PlayerPrefs.GetInt(highScore2$$anonymous$$ey,0); //retrieves the highscore numbers
     updateHighScore3 = PlayerPrefs.GetInt(highScore3$$anonymous$$ey,0);
 }

 void Update () {

     if(highScore1 >= userHighScore);{
         StartCoroutine(UpdateScore());
         userName = PlayerPrefs.GetString(userHighScoreTextSave, "Empty"); //grabs the usernname text
         userHighScore = PlayerPrefs.GetInt(highScore$$anonymous$$ey,0); //grabs the new highscore
     }

 }

 IEnumerator UpdateScore(){

     highScore2 = highScore3;
     highScore2 = highScore1;
     highScore1 = userHighScore;

     userName2 = userName3;
     userName2 = userName1;
     userName1 = userName;

     //sets highscore 3
     PlayerPrefs.SetString(highScore3Text$$anonymous$$ey, userName3);
     PlayerPrefs.SetInt(highScore3$$anonymous$$ey, highScore3);
     PlayerPrefs.Save();
 
     yield return new WaitForSeconds(1);
     //sets highscore 2
     PlayerPrefs.SetString(highScore2Text$$anonymous$$ey, userName2);
     PlayerPrefs.SetInt(highScore2$$anonymous$$ey, highScore2);
     PlayerPrefs.Save();
     yield return new WaitForSeconds(1);
     //sets highscore 3
     PlayerPrefs.SetString(highScore1Text$$anonymous$$ey, userName1);
     PlayerPrefs.SetInt(highScore1$$anonymous$$ey, highScore1);
     PlayerPrefs.Save();
     Start(); //updates the score
     yield return new WaitForSeconds(1);

 }

This is my full script, high score 1 and 2 display but like before they display the highest score... High score 3 displays no text and a score of zero. I have have been debugging too.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Retrieving online highscores (following a guide) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to implement online leader board to my project? 0 Answers

Implementing a leader board / high score (File read / write) 7 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