Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 06, 2017 at 12:41 PM by lsp34 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by lsp34 · Dec 05, 2017 at 01:04 PM · unity 2dplayerprefsif-statementshighscoresscores

how to make high scores using playerprefs

hello, I'm having a problem regarding in making a high score list... I'm not fully understand the "if" statements, please help me to solve my problem..

there will be 3 to be stored in the playerprefs... then it will just sort from the highest to the third high score.. this is my code..

 void HighScores () {
 
         if (player.score >= highScore1 && player.score > highScore2 && player.score > highScore3) {
             highScore3 = highScore2;
             PlayerPrefs.SetInt (highScore_3, highScore3);
             highScore2 = highScore1;
             PlayerPrefs.SetInt (highScore_2, highScore2);
             highScore1 = player.score;
             PlayerPrefs.SetInt (highScore_1, highScore1);
         } else if (player.score < highScore1 && player.score >= highScore2 && player.score > highScore3) {
             highScore3 = highScore2;
             PlayerPrefs.SetInt (highScore_3, highScore3);
             highScore2 = player.score;
             PlayerPrefs.SetInt (highScore_2, highScore2);
         } else if (player.score < highScore1 && player.score < highScore2 && player.score > highScore3) {
             highScore3 = player.score;
             PlayerPrefs.SetInt (highScore_3, highScore3);
         }
             
     }


example my current score is 5, then the highScores output are 5,5,0... i don't know why the second variable is 5... the output must become like this, 5,0,0... if i play again and example i gain 10pts... the output must become 10,5,0 but it became 10,10,0 ..

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 Yeezyy · Dec 05, 2017 at 02:17 PM 0
Share

Try void HighScore(){ switch(player.score){ case 0: If(player.score>=highScore1) //set to highscore_1 break; case 1: If(player.score>=highScore2) //set to highscore_2 break; case 2: If(player.score>=highScore3) //set to highscore_3 break; default : break; } }

2 Replies

  • Sort: 
avatar image
0

Answer by ShadyProductions · Dec 05, 2017 at 02:06 PM

Why do you make it so complicating?

 public void Highscores()
 {
     // Get latest highscore
     var first = PlayerPrefs.GetInt(highScore_1);
     var second = PlayerPrefs.GetInt(highScore_2);
 
     // Push first to second, second to third
     PlayerPrefs.SetInt(highScore_2, first);
     PlayerPrefs.SetInt(highScore_3, second);
 
     // Update first
     PlayerPrefs.SetInt(highScore_1, player.score);
 }
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
0

Answer by Bunny83 · Dec 05, 2017 at 02:15 PM

I don't think the error is in the saving part but rather in the loading code. Maybe you have a copy&paste error and you load the first highscore value into highScore1 and highScore2 ?


Though the saving code could be quite simplified as you have a lot unnecessary and redundant conditions.

 if (player.score >= highScore1)
 {
     highScore3 = highScore2;
     highScore2 = highScore1;
     highScore1 = player.score;
 
     PlayerPrefs.SetInt (highScore_3, highScore3);
     PlayerPrefs.SetInt (highScore_2, highScore2);
     PlayerPrefs.SetInt (highScore_1, highScore1);
 }
 else if (player.score >= highScore2)
 {
     highScore3 = highScore2;
     highScore2 = player.score;
 
     PlayerPrefs.SetInt (highScore_3, highScore3);
     PlayerPrefs.SetInt (highScore_2, highScore2);
 }
 else if (player.score > highScore3)
 {
     highScore3 = player.score;
     PlayerPrefs.SetInt (highScore_3, highScore3);
 }

Since the high scores are always sorted from largest to lowest we don't have to check if the new score is greater than the second and third if it's already larger than the first one. Since we have an "else if" chain only one statement can be executed. So for the second you don't have to check that the score is smaller than the first because you only reach the second if statement in case the first was not true.


Anyways your actual problem is not in the saving code.

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

Follow this Question

Answers Answers and Comments

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

Related Questions

PlayerPrefs Not Working 0 Answers

how to creat and scoring and high score system for an endless runner,how do I display my score on a game over scene and also create and high score system for the same ? 0 Answers

How can I store highscores in PlayerPrefs? 3 Answers

If statement bug? (Glitch) 1 Answer

PlayerPrefs not saving my variable value for HighScore System 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