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 iamorim · Feb 09, 2013 at 09:12 PM · c#playerprefshighscoreranking

PlayerPrefs highscore system?

Hi! I'm trying to make a highscore system and I read a lot of questions here and it's almost done. I'm using this example: http://answers.unity3d.com/questions/20773/how-do-i-make-a-highscores-board.html

I have this:

 if(GUILayout.Button("Salvar pontuacao!")){
             PlayerPrefs.SetString("High Name", name);
             PlayerPrefs.SetInt("High Score", score);
             Application.LoadLevel(2);
     }

And trying to show it in a GUI with these codes:

 void AddScore(string name, int score){
         newScore = score;
         newName = name;
         
         for(int i=0; i<10; i++){
             if(PlayerPrefs.HasKey(i+"High Score")){
                 if(PlayerPrefs.GetInt(i+"High Score") < newScore){
                     //se o newScore for maior que o antigo
                     oldScore = PlayerPrefs.GetInt(i+"High Score");
                     oldName = PlayerPrefs.GetString(i+"High Name");
                     PlayerPrefs.SetInt(i+"High Score", newScore);
                     PlayerPrefs.SetString(i+"High Name", newName);
                     newScore = oldScore;
                     newName = oldName;
                 }
             }else{
                 PlayerPrefs.SetInt(i+"High Score", newScore);
                 PlayerPrefs.SetString(i+"High Name", newName);
                 newScore = 0;
                 newName = "";
               }
         }
     }

Here:

 firstName = PlayerPrefs.GetString("0High Name", newName);
         firstScore = PlayerPrefs.GetInt("0High Score", newScore);
         secondName = PlayerPrefs.GetString("1High Name", newName);
         secondScore = PlayerPrefs.GetInt("1High Score", newScore);
         thirdName = PlayerPrefs.GetString("2High Name", newName);
         thirdScore = PlayerPrefs.GetInt("2High Score", newScore);
         fourthName = PlayerPrefs.GetString("3High Name", newName);
         fourthScore = PlayerPrefs.GetInt("3High Score", newScore);
         fifthName = PlayerPrefs.GetString("4High Name", newName);
         fifthScore = PlayerPrefs.GetInt("4High Score", newScore);
         sixthName = PlayerPrefs.GetString("5High Name", newName);
         sixthScore = PlayerPrefs.GetInt("5High Score", newScore);
         seventhName = PlayerPrefs.GetString("6High Name", newName);
         seventhScore = PlayerPrefs.GetInt("6High Score", newScore);
         eighthName = PlayerPrefs.GetString("7High Name", newName);
         eighthScore = PlayerPrefs.GetInt("7High Score", newScore);
         ninthName = PlayerPrefs.GetString("8High Name", newName);
         ninthScore = PlayerPrefs.GetInt("8High Score", newScore);
         tenthName = PlayerPrefs.GetString("9High Name", newName);
         tenthScore = PlayerPrefs.GetInt("9High Score", newScore);

 GUI.Box(new Rect(Screen.width/2 - 50, 50, 100, 20), firstName);
         GUI.Box(new Rect(Screen.width/2 - 50, 100, 100, 20), secondName);
         GUI.Box(new Rect(Screen.width/2 - 50, 150, 100, 20), thirdName);
         GUI.Box(new Rect(Screen.width/2 - 50, 200, 100, 20), fourthName);
         GUI.Box(new Rect(Screen.width/2 - 50, 250, 100, 20), fifthName);
         GUI.Box(new Rect(Screen.width/2 - 50, 300, 100, 20), sixthName);
         GUI.Box(new Rect(Screen.width/2 - 50, 350, 100, 20), seventhName);
         GUI.Box(new Rect(Screen.width/2 - 50, 400, 100, 20), eighthName);
         GUI.Box(new Rect(Screen.width/2 - 50, 450, 100, 20), ninthName);
         GUI.Box(new Rect(Screen.width/2 - 50, 500, 100, 20), tenthName);

 GUI.Box(new Rect(Screen.width/2 + 100, 50, 50, 20), firstScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 100, 50, 20), secondScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 150, 50, 20), thirdScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 200, 50, 20), fourthScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 250, 50, 20), fifthScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 300, 50, 20), sixthScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 350, 50, 20), seventhScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 400, 50, 20), eighthScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 450, 50, 20), ninthScore.ToString());
         GUI.Box(new Rect(Screen.width/2 + 100, 500, 50, 20), tenthScore.ToString());

Please, I already try a lot of things but I'm stuck. Thanks!

Comment
Add comment · Show 8
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 OWiz · Feb 09, 2013 at 09:13 PM 0
Share

Where's the problem?

avatar image iamorim · Feb 10, 2013 at 04:16 AM 0
Share

It doesn't work. The score and name just show up in the GUI if I put a number here:

 if(GUILayout.Button("Salvar pontuacao!")){
          PlayerPrefs.SetString("0High Name", name);
          PlayerPrefs.SetInt("0High Score", score);
          Application.LoadLevel(2);
     }

Doing it the score show at the first position. But if I play again the new score will show at the first position too.

avatar image Lockstep · Feb 10, 2013 at 04:39 AM 0
Share

one thing that catches my eye is that GetString and GetInt only take one argument but you pass two. Also I'd suggest to store these in an array ins$$anonymous$$d of having 10 single variables. You could define a class which contains a name and a score then make an array of this class. If you pack the array in another class, you can save it easily with this little skrip.

avatar image Lockstep · Feb 10, 2013 at 01:36 PM 1
Share

If you do this the first score gets overridden no matter what. I kinda assumed you would call your AddScore function at some place. Have the Button call this function ins$$anonymous$$d of onlyl overriding the first score.

avatar image Lockstep · Feb 10, 2013 at 05:53 PM 1
Share

In this case you can do it with a public static function. Declare AddScore as static and public:

 public static void AddScore(string name, int score){
      //your code
 }

The you can call it from everywhere by using:

 ranking.AddScore(someString, someInt);
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

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

Distribute terrain in zones 3 Answers

Can someone help me with my highscore script? 2 Answers

How do I save the high score from the game scene onto the main menu scene using PlayerPrefs and Dontdestroyonload? 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