0
\$\begingroup\$

I am trying to create a highscore value that is stored in playerprefs, and only updates when a higher score is achieved. I got playerprefs viewer on the asset store, and can see that it is holding a value, but it seems that it blows through the if statement that checks to see if the new value is a highscore. This means that no matter what score I get, it always stores it as a highscore.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform Player;

    public Text scoreText, Highscoretext;

    public GameObject Postgame, Ingame;

    private bool stopScore = false;

    void Update()
    {
        if (stopScore == false)
        {
            //Keeps track of and displays current score
            float floatscore = Player.position.x;
            int intscore = (int)floatscore / 10;
            if (FollowPlayer.gameOver == false)
            {
                scoreText.text = intscore.ToString();
            }

            if (FollowPlayer.gameOver == true)   // Checks wheather the game has ended (from a different script)
            {
                stopScore = true;   // Stops running the score function

                if (intscore > (PlayerPrefs.GetInt("HighScore")));   // Checks to see if the score of the current game is a new Highscore
                {
                    PlayerPrefs.SetInt("HighScore", intscore);   // Sets new Highscore

                    EndScreen();   // Function to load the endscreen
                }
            }
        }      
    }
    public void EndScreen()
    {

        Highscoretext.text = "Highscore: " + PlayerPrefs.GetInt("HighScore").ToString();   // Supposed to set the endscreen Highscore value to the new Highscore value

        // Different canvas overlays
        Postgame.gameObject.SetActive(true);
        Ingame.gameObject.SetActive(false);
    }
}

I tried using Debug.Log to debug this, and found that even if intscore is smaller than PlayerPrefs.GetInt("HighScore")), it will still run the code that sets a new highscore. Anyone have any idea of how to fix this?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Did you try to set breakpoints to see what is exactly happening? Another advice, try to dont run this code on Update. You can make an event called when the game is over. Subscribe to this event and call your score saving code. \$\endgroup\$ – Ben Jun 15 '20 at 11:27
2
\$\begingroup\$

You need to remove the semicolon from this line:

if (intscore > (PlayerPrefs.GetInt("HighScore"))); 

The semicolon in that position ends the if/then block, making it essentially an if without a then. The following block where you assign to PlayerPref then gets run regardless of the if, since it is just a code block with superfluous braces, but completely unrelated to the if syntactically.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.