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();
    }
}

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”);

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

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;