Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question PlayerPrefs

Discussion in 'Scripting' started by jeti20, Dec 2, 2022.

  1. jeti20

    jeti20

    Joined:
    Aug 21, 2021
    Posts:
    12
    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 :/ I understand this like this: The highest score cannot change if the _waveNumber is smaller than PlayerPrefs.GetInt("HighScore")).... but it changed anyway

    Link:


    Code (CSharp):
    1.  public void Update()
    2.     {      
    3.         _enemyCount = FindObjectsOfType<Enemy>().Length;
    4.      
    5.         //checks if enemy == 0, if yes resp new wave +1enemy
    6.         if (_enemyCount == 0 && _playercontroller.Alive == true)
    7.         {
    8.             _waveNumber++;
    9.             ChceckHighScore();
    10.             SpawnEnemyWave(_waveNumber);                
    11.             Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
    12.         }      
    13.         UpdatingHighScore();
    14.     }
    15.  
    16.     //sprawdzanie największego wyniku
    17.     public void ChceckHighScore()
    18.     {
    19.         if (_waveNumber > PlayerPrefs.GetInt("HighScore"));
    20.         {
    21.             PlayerPrefs.SetInt("HighScore", _waveNumber);
    22.         }
    23.     }
    24.  
    25.     //wyświetlanie wyniku
    26.     void UpdatingHighScore()
    27.     {
    28.         scoreTextInGame.GetComponent<TextMeshProUGUI>().text = "Wave: " + _waveNumber;
    29.     }
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Code (CSharp):
    1. var SCORE = PlayerPrefs.GetInt("HighScore");
    2. Debug.Log(SCORE);
     
  3. jeti20

    jeti20

    Joined:
    Aug 21, 2021
    Posts:
    12
    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);"
    Code (CSharp):
    1. public int score;
    2.  
    3.     [SerializeField] PlayerController _playercontroller;
    4.  
    5.     void Start()
    6.     {
    7.         SpawnEnemyWave(_waveNumber);
    8.      
    9.         //spawnujemy powerup'y w losowych miejscach
    10.         Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
    11.         score = PlayerPrefs.GetInt("HighScore");
    12.     }
    13.  
    14.     public void Update()
    15.     {
    16.         //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)
    17.         _enemyCount = FindObjectsOfType<Enemy>().Length;
    18.      
    19.         //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
    20.         if (_enemyCount == 0 && _playercontroller.Alive == true)
    21.         {
    22.             _waveNumber++;
    23.             ChceckHighScore();
    24.             SpawnEnemyWave(_waveNumber);  
    25.             //za każdą falą spawnuje sie jeden powerup
    26.             Instantiate(_powerUpPrefab, GenerateSpawnPositionEnemy() + new Vector3(0, 1, 0), _powerUpPrefab.transform.rotation);
    27.         }
    28.      
    29.         UpdatingHighScore();
    30.     }
    31.  
    32.     //sprawdzanie największego wyniku
    33.     public void ChceckHighScore()
    34.     {
    35.         if (_waveNumber > score);
    36.         {
    37.             PlayerPrefs.SetInt("HighScore", _waveNumber);
    38.         }
    39.     }
    40.  
    41.     //wyświetlanie wyniku
    42.     void UpdatingHighScore()
    43.     {
    44.         scoreTextInGame.GetComponent<TextMeshProUGUI>().text = "Wave: " + _waveNumber;
    45.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    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:

    Code (csharp):
    1. TheBest.RecordScoreIfHigher( lastGamePlayScore);
    For the lowest time: https://pastebin.com/A7GC76uQ

    Usage:

    Code (csharp):
    1. TheBest.RecordTimeIfLower( lastGamePlayTime);
    To retrieve the best score or time, use one of these:

    Code (csharp):
    1. int bestScore = TheBest.BestScore;
    Code (csharp):
    1. float bestTime = TheBest.BestTime;
    Full usage example:

    https://pastebin.com/ygFAvNsM