• 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 Hobene · Apr 19, 2020 at 08:53 AM · playerprefshighscoreshighscore

My 3 best high scores are always the same. I always see the 1 best score. Can't get them to change accordingly.

I made a fairly simple high scrore system that should showcase the latest 3 best scores but I can't seem to make the second and the third score change.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Scores : MonoBehaviour
 {
     public Text yourCurrentScoreNumberText;
     public Text score1;
     public Text score2;
     public Text score3;
 
     
     public Canvas HighScoreCanvas;
     private TreeBehavior treeBehavior;
     public GameObject ScoreCounter;
 
     void Start()
     {           
             HighScoreCanvas.enabled = false;   
             //DontDestroyOnLoad(gameObject);     
     }  
     public void ShowHighScoreCanvas()
     {      
         if (ScoreCounter.GetComponent<ScoreCounter>().mängLäbi == true)
         {                       
             HighScoreCanvas.enabled = true;
 
             yourCurrentScoreNumberText.text = ScoreCounter.GetComponent<ScoreCounter>().finalScore.ToString();
             
             int score1st = PlayerPrefs.GetInt("bestscore");
             int score2nd = PlayerPrefs.GetInt("secondscore");
             int score3rd = PlayerPrefs.GetInt("thirdscore");
                                                         
             if (ScoreCounter.GetComponent<ScoreCounter>().finalScore >= score1st)
             {
                 score3rd = score2nd;
                 score2nd = score1st;
                 score1st = ScoreCounter.GetComponent<ScoreCounter>().finalScore;
 
                 PlayerPrefs.SetInt("thirdscore", score3rd);
                 PlayerPrefs.SetInt("secondscore", score2nd);
                 PlayerPrefs.SetInt("bestscore", score1st);
 
                 score1.text = score1st.ToString();
                 score2.text = score2nd.ToString();
                 score3.text = score3rd.ToString();
 
             }
             else if(ScoreCounter.GetComponent<ScoreCounter>().finalScore >= score2nd) 
             {
                 score3rd = score2nd;
                 score2nd = ScoreCounter.GetComponent<ScoreCounter>().finalScore;
 
                 PlayerPrefs.SetInt("thirdscore", score3rd);
                 PlayerPrefs.SetInt("secondscore", score2nd);
 
                 score2.text = score2nd.ToString();
                 score3.text = score3rd.ToString();
             }
             else if(ScoreCounter.GetComponent<ScoreCounter>().finalScore >= score3rd)
             {
                 score3rd = ScoreCounter.GetComponent<ScoreCounter>().finalScore;
                 PlayerPrefs.SetInt("thirdscore", score3rd);
 
                 score3.text = score3rd.ToString();
             }           
         }  
     }   
     public void playButton()
     {
         SceneManager.LoadScene(1);
         HighScoreCanvas.enabled = false;
     }
     public void mainMenu()
     {
         SceneManager.LoadScene(0);
         HighScoreCanvas.enabled = false;
     }   
     void Update()
     {
         ShowHighScoreCanvas();
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hobene · Apr 21, 2020 at 09:28 AM

Fixed the problem by creating a bool that can have a state change only once and added the script inside of it. Not sure why it's working like this but nevermind.

First I created a bool

 private bool singleExecution;


then for my public void ShowHighScoreCanvas()

I put a if (!singleExecution) infront of if (ScoreCounter.GetComponent<ScoreCounter>().mängLäbi == true) "MY CODE" and in the end singleExecution = true;

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 dajohnso · Apr 21, 2020 at 05:52 PM 0
Share

Glad it worked out. I also read elsewhere that if you declare the variable as static there can only be 1 copy otherwise there could be multiple copies per object, instance, etc... depending on what the script is attached to.

avatar image
1

Answer by dajohnso · Apr 20, 2020 at 04:34 AM

Bugs aside...I would store the high_scores as an array of 4 (int) items but only show the top 3. When a new score is achieved, add the current score to the array in spot 4 and sort the list by score (with sort function). Its a lot less code then you have above, and allows for using loops instead of lines.

Bug wise I would check the values returned by int score1st = PlayerPrefs.GetInt("bestscore"); int score2nd = PlayerPrefs.GetInt("secondscore"); int score3rd = PlayerPrefs.GetInt("thirdscore");

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 logicandchaos · Apr 19, 2020 at 04:45 PM

you should use some debug logs to make sure all the score values are correct

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 Hobene · Apr 20, 2020 at 11:13 AM 0
Share

All 3 values are the best score. I can't seem to fix it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

132 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 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

PlayerPref set int and get int 1 Answer

How can I save highscores and display them for later? 1 Answer

Timer highscore trouble 1 Answer

Can someone help me with my highscore script? 2 Answers

Player Prefs question. 1 Answer


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