Answer the question
In order to leave comments, you need to log in
Updating the best score (highscore) occurs every time Unity, How can I decide?
Guys, my highscore works fine, only now it is updated every other time. For example: highscore is 5 and in the game I score 10 points and lose then on the highscore inscription it still writes 5, but after I clicked a new game (restart) and lost without breaking the record (highscore), this time the highscore is updated and writes 10. How can I solve this?
public Text highScoreText;
public Text scoreText;
public Text finalScoreText;
private int scoreNum;
private int easyHighScoreNum;
private int mediumHighScoreNum;
private int hardHighScoreNum;
private void Start()
{
scoreNum = 0;
scoreText.text = scoreNum.ToString();
finalScoreText.text = scoreNum.ToString();
if (PlayerPrefs.HasKey("easy"))
{
easyHighScoreNum = PlayerPrefs.GetInt("easyHighScore", 0);
}
else if (PlayerPrefs.HasKey("medium"))
{
mediumHighScoreNum = PlayerPrefs.GetInt("mediumHighScore", 0);
}
else if (PlayerPrefs.HasKey("hard"))
{
hardHighScoreNum = PlayerPrefs.GetInt("hardHighScore", 0);
}
}
private void Update()
{
if (PlayerPrefs.HasKey("easy"
highScoreText.text = easyHighScoreNum.ToString();
}
else if (PlayerPrefs.HasKey("medium"))
{
highScoreText.text = mediumHighScoreNum.ToString();
}
else if (PlayerPrefs.HasKey("hard"))
{
highScoreText.text = hardHighScoreNum.ToString();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("PresentObj"))
{
scoreNum++;
scoreText.text = scoreNum.ToString();
finalScoreText.text = scoreNum.ToString();
if (PlayerPrefs.HasKey("easy"))
{
if (easyHighScoreNum < scoreNum)
{
PlayerPrefs.SetInt("easyHighScore", scoreNum);
}
}
else if (PlayerPrefs.HasKey("medium"))
{
if (mediumHighScoreNum < scoreNum)
{
PlayerPrefs.SetInt("mediumHighScore", scoreNum);
}
}
else if (PlayerPrefs.HasKey("hard"))
{
if (hardHighScoreNum <
PlayerPrefs.SetInt("hardHighScore", scoreNum);
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question