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 AusieJamster · Mar 10, 2014 at 12:31 PM · c#playerprefsscore

Highscores PlayerPrefs Sorting Issue

I need to make this class make a high scores table and make it so that when a new score is enter it goes ahead of the old one but I'm having a lot of trying trouble to figure it out, can someone help? using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class SaveLoad
 {
     public static string name;
 
     static List<HighscoreHolder> arr_score = new List<HighscoreHolder>();
 
     public static void Save ()
     {
         Load(); // Load current highscore list for comparison
         
         if (GameManager.score > score[4].playerScore)// If you get a high score
         {
             name = score[5].playerName; //input player name into temp highscore
 
             score[5].playerScore = GameManager.score;
             
             //Sort array by score
             arr_score.Add(new HighscoreHolder { playerName = name, playerScore = GameManager.score });
             arr_score.Sort(s1,s2) => s2.arr_score == s1.arr_score ? 0 : (s2.arr_score < s1.arr_score ? 1 : -1));
             while(arr_score.Count >= 6)
                 arr_score.RemoveAt(5);
 
             //Put Score array top 5 back into player prefs
             for (int i = 0; i < 5; i++)
             {            
                 PlayerPrefs.SetString("Name" + i, score[i].playerName);
                 PlayerPrefs.SetInt("Score" + i, score[i].playerScore);
             }
         }
     }
     
     public static void Load ()
     {
         score = new HighscoreHolder[6];
         for (int i = 0; i < 5; i++)
         {            
             score[i].playerName = PlayerPrefs.GetString("Name" + i);
             score[i].playerScore = PlayerPrefs.GetInt("Score" + i);                
         }
     }
 
     public static void OnGUI ()
     {
         SaveLoad.Load();
 
         GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f, 50, 20), score[0].playerName + ": " + score[0].playerScore);
         GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height / 10, 50, 20), score[1].playerName + ": " + score[1].playerScore);
         GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 2 / 10, 50, 20), score[2].playerName + ": " + score[2].playerScore);
         GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 3 / 10, 50, 20), score[3].playerName + ": " + score[3].playerScore);
         GUI.Label(new Rect(Screen.width/2.25f, Screen.height/2.5f + Screen.height * 4/ 10, 50, 20), score[4].playerName + ": " + score[4].playerScore);
     }
 }
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
Best Answer

Answer by whydoidoit · Mar 10, 2014 at 01:37 PM

So use a List and insert the new score, sort it and remove the 6th element if there is one.

    using System.Collections.Generic;
    ...
    static List<HighscoreHolder> score = new List<HighscoreHolder>();
    ...

    score.Add(new HightscoreHolder { name = currentPlayerName, score=GameManager.score });
    score.Sort((s1,s2)=>s2.score ==  s1.score ? 0 : (s2.score > s1.score ? 1 : -1));
    while(score.Count >= 6)
        score.RemoveAt(5);
Comment
Add comment · Show 4 · 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 AusieJamster · Mar 10, 2014 at 02:28 PM 0
Share

I was looking at Lists before but i was having trouble using them in unity it doesn't seem to give me any code help for it and then it sort of accepts it sort of doesn't "Assets/Scripts/SaveLoad.cs(22,44): error CS1525: Unexpected symbol `=>'"

avatar image whydoidoit · Mar 10, 2014 at 02:30 PM 0
Share

Lists work fine in Unity if you include System.Collections.Generic. Not sure why you Lambda isn't working - can you post exactly what you have?

avatar image AusieJamster · Mar 10, 2014 at 03:05 PM 0
Share

Just updated the code onto the question, thanks for the help :)

avatar image AusieJamster · Mar 10, 2014 at 03:07 PM 0
Share

Woops totally noticed where i stuffed up sorry i missed a ( :(

avatar image
0

Answer by AnXgotta · Mar 10, 2014 at 12:42 PM

Here is some seudo-code to point you in the right direction

 // Get array of current leader board
 // Check if new HighScore is greater than lowest highScore on leader board
 // if yes
     // find index of new highScore in the leader board
         // you could do this one of many ways (loop and check for example)
     // bump other highScores down the leader board from determined index
     // add new highScore to its index on leader board
     // save new high scores    
 // if no
     // do nothing
Comment
Add comment · Show 2 · 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 AusieJamster · Mar 10, 2014 at 01:33 PM 0
Share

I think where most of my problem lays is with how to (bump other highScores down the leader board from deter$$anonymous$$ed index) push doesn't work and i'm not sure how to sort a 2D array

avatar image AnXgotta · Mar 10, 2014 at 03:02 PM 0
Share

I see, sorting an array containing your 'HighscoreHolder' object is the problem. I would look at 'whydoidoit' answer. Implementing your own sorting mechanism using a List is a very straightforward way to do this.

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

22 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 avatar image avatar image

Related Questions

Problem with High Score System using PlayerPrefs [C#] 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Multiple Cars not working 1 Answer

I'm trying to set a high score but I can't display it in another scene? 2 Answers

What am I doing wrong with PlayerPrefs? 3 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