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 spacelyjoe · Feb 14, 2014 at 08:46 AM · highscoresscores

How to update high scores using PlayerPrefs?

 if (count > PlayerPrefs.GetInt("Score1")){
     PlayerPrefs.SetInt("Score2", PlayerPrefs.GetInt("Score1"));
     PlayerPrefs.SetInt("Score1", count);
 }
 else if (count > PlayerPrefs.GetInt("Score2")){
     PlayerPrefs.SetInt("Score3", PlayerPrefs.GetInt("Score2"));
     PlayerPrefs.SetInt("Score2", count);
 }
 else if (count > PlayerPrefs.GetInt("Score3")){
     PlayerPrefs.SetInt("Score4", PlayerPrefs.GetInt("Score3"));
     PlayerPrefs.SetInt("Score3", count);
 }
 else if (count > PlayerPrefs.GetInt("Score4")){
     PlayerPrefs.SetInt("Score5", PlayerPrefs.GetInt("Score4"));
     PlayerPrefs.SetInt("Score4", count);
 }
 else if (count > PlayerPrefs.GetInt("Score5")){
     PlayerPrefs.SetInt("Score6", PlayerPrefs.GetInt("Score5"));
     PlayerPrefs.SetInt("Score5", count);
 }
 else if (count > PlayerPrefs.GetInt("Score6")){
     PlayerPrefs.SetInt("Score7", PlayerPrefs.GetInt("Score6"));
     PlayerPrefs.SetInt("Score6", count);
 }
 else if (count > PlayerPrefs.GetInt("Score7")){
     PlayerPrefs.SetInt("Score8", PlayerPrefs.GetInt("Score7"));
     PlayerPrefs.SetInt("Score7", count);
 }
 else if (count > PlayerPrefs.GetInt("Score8")){
    PlayerPrefs.SetInt("Score9", PlayerPrefs.GetInt("Score8"));
    PlayerPrefs.SetInt("Score8", count);
 }
 else if (count > PlayerPrefs.GetInt("Score9")){
     PlayerPrefs.SetInt("Score10", PlayerPrefs.GetInt("Score9"));
     PlayerPrefs.SetInt("Score9", count);
 }
 else if (count > PlayerPrefs.GetInt("Score10")){
     PlayerPrefs.SetInt("Score11", PlayerPrefs.GetInt("Score10"));
     PlayerPrefs.SetInt("Score10", count);    
 }


So I want it to update for when the count is higher than the already set highscore, but to do that I would need to make the high score pass down to the score below it, but then I'd need to do that for every score below it as well. How can I recode this to make this easily implemented? Cause the way I have it now only passes the score you beat down. *say you score #2 highest score, your old #2 score passes down to #3 and then the old #3 score is just replaced but isn't passed down as #4.

Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RyanZimmerman87 · Feb 14, 2014 at 09:04 AM

It seems like you are on the right track already with the initial replacements, now you just need to do the rest.

Depending on how many high scores you are trying to store you could just do the exact ones right there in those else ifs you already have.

Ideally you would just set up a function which receives the high score you set to know what to do.

For Example:

 else if (count > PlayerPrefs.GetInt("Score3")){
     PlayerPrefs.SetInt("Score4", PlayerPrefs.GetInt("Score3"));
     PlayerPrefs.SetInt("Score3", count);
 
 //send this function with the high score you just set in this case #3
 int highScoreRank = 3;
 setRemainingHighScoresFunction(highScoreRank);
 }
 
 
 //calls this function
 void setRemainingHighScoresFunction(int newHighScoreRank)
 {
 
 //get the scores
 int oldScore;
 int newScore;
 
 //so for example if you want to record the top 10 it would be
 int highestRecordedRank = 10;
 
 
 for (int i = newHighScoreRank; i < highestRecordedRank - 1; ++i)
 {
 //previous rank possibilities 
 
 //example starting spot
 else if (int i == 3)
 {
 
 
 oldScore = PlayerPrefs.GetInt("Score4");
 
 //if score does not tie
 if (newScore != oldScore)
 {
 PlayerPrefs.SetInt("Score5", oldScore);
 
 newScore = oldScore;
 }

 //if you don't want duplicate scores entries for adjacent high scores you would return here
 else return;
 
 //if you do want duplicate score entries this example is a little goofed up my bad
 
 }

 //do rest of possibilities for example:
 //else if (int i == 4)
 
 }

I'm not really willing to double check any of that logic at the moment, and I've been getting into my beer programming stage for the night. So I would not be surprised one bit if there are errors with the specifics. But that should hopefully help you a little to get started.

Hmm not sure if you want to record duplicate scores of the same value as high scores, that might be a mistake in this example so you could just remove the part:

 if (newScore != oldScore)
 {

Also with this example if you don't want duplicate scores you would need to do the first

 if (newScore != oldScore)
     {

BEFORE you call the function since the first comparison in this example could not detect it correctly.

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

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

19 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

Related Questions

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

In-game high score system 1 Answer

Facebook API: How to get all app users scores 1 Answer

how to make high scores using playerprefs 2 Answers

MySQL Database Highscore 0 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