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:
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;
}
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;
}