- Home /
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();
}
}
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;
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.
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");
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
Your answer
Follow this Question
Related Questions
Timer highscore trouble 1 Answer
Can someone help me with my highscore script? 2 Answers
PlayerPref set int and get int 1 Answer
How can I save highscores and display them for later? 1 Answer
Saving Times as Highscore 1 Answer