PlayerPrefs

I have, a problem with playerPrefs… I’m struggling with this a lot in last days and cannot solve this. When I have the highest score e.g. 5th wave, I start the game and go to 2nd wave then the HighScore changes to two… Idk where is the mistake :confused: I understand this like this: The highest score cannot change if the _waveNumber is smaller than PlayerPrefs.GetInt(“HighScore”))… but it changed anyway

Link:

 public void Update()
    {       
        _enemyCount = FindObjectsOfType<Enemy>().Length;
      
        //checks if enemy == 0, if yes resp new wave +1enemy
        if (_enemyCount == 0 && _playercontroller.Alive == true)
        {
            _waveNumber++;
            ChceckHighScore();
            SpawnEnemyWave(_waveNumber);                
            Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
        }      
        UpdatingHighScore();
    }

    //sprawdzanie największego wyniku
    public void ChceckHighScore()
    {
        if (_waveNumber > PlayerPrefs.GetInt("HighScore"));
        {
            PlayerPrefs.SetInt("HighScore", _waveNumber);
        }
    }

    //wyświetlanie wyniku
    void UpdatingHighScore()
    {
        scoreTextInGame.GetComponent<TextMeshProUGUI>().text = "Wave: " + _waveNumber;
    }
var SCORE = PlayerPrefs.GetInt("HighScore");
Debug.Log(SCORE);

So I did changes and it looks like this but still the highest score is overwrriten by new smaller wave. Added var score and added score = PlayerPrefs.GetInt(“HighScore”); in start method also changed “if (_waveNumber > score);”

public int score;

    [SerializeField] PlayerController _playercontroller;

    void Start()
    {
        SpawnEnemyWave(_waveNumber);
      
        //spawnujemy powerup'y w losowych miejscach
        Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
        score = PlayerPrefs.GetInt("HighScore");
    }

    public void Update()
    {
        //sięgamy do skryptu Enemy aby sprawdzić ilośc wrogów na podstawie przypisanych do nich skryptu Enemy, albo tagu, jesli zepchniemy i zniknie jeden z trzech to wyświetli 2 (real time)
        _enemyCount = FindObjectsOfType<Enemy>().Length;
     
        //sprawdza, czy wszyscy wrogowie zostali zepchnięci (ich liczba == 0) i jeśli tak to wykonuje inkementracje do wavenumber któa decyduje o tym ile zrespi się nowych przeciwników w fali
        if (_enemyCount == 0 && _playercontroller.Alive == true)
        {
            _waveNumber++;
            ChceckHighScore();
            SpawnEnemyWave(_waveNumber);   
            //za każdą falą spawnuje sie jeden powerup
            Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
        }
      
        UpdatingHighScore();
    }

    //sprawdzanie największego wyniku
    public void ChceckHighScore()
    {
        if (_waveNumber > score);
        {
            PlayerPrefs.SetInt("HighScore", _waveNumber);
        }
    }

    //wyświetlanie wyniku
    void UpdatingHighScore()
    {
        scoreTextInGame.GetComponent<TextMeshProUGUI>().text = "Wave: " + _waveNumber;
    }

Generally try to avoid splattering PlayerPrefs calls all over your codebase:

Instead, centralize it properly to you can access it just like any other variable.

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

Useful for a relatively small number of simple values.

Tracking simple high score / low time single-entry leaderboard:

For the highest score: https://pastebin.com/VmngEK05

Usage:

TheBest.RecordScoreIfHigher( lastGamePlayScore);

For the lowest time: https://pastebin.com/A7GC76uQ

Usage:

TheBest.RecordTimeIfLower( lastGamePlayTime);

To retrieve the best score or time, use one of these:

int bestScore = TheBest.BestScore;
float bestTime = TheBest.BestTime;

Full usage example:

https://pastebin.com/ygFAvNsM