I’m expanding on the 3D Space Shooter project from the tutorial, but I’m having some difficulty with saving and loading a high score IF the regular score variable is set to 0 in the Start Function. In other words, what happens is… the high score only gets successfully saved/loaded if I set my variable “start” to PlayerPrefs.GetInt(“High Score”).
However, the problem with this is that is that my regular score counts up to what my high score was: If my high score was 1080 for example, I reload the scene, and then as soon as I shoot one asteroid, I gain 1080 points when I’m only supposed to gain 10. This seems to be the only way I can use a previously saved high score though.
I’ve tried creating a separate variable in the Start function called “highscore” and setting THAT to PlayerPrefs.GetInt(“High Score”), while also setting score to 0 but that doesn’t enable the high score to save and load the next time I reload the scene. Could somebody tell me what’s wrong with my GameController script (script where the high score is supposed to fit in)?
BEFORE ANYONE ASKS ME IF I READ THE DOCUMENTATION OR LOOKED AT SIMILAR QUESTIONS ON HERE, I DID. I looked at how one puts PlayerPrefs.GetInt and PlayerPrefs.SetInt but for whatever reason, it’s not working. I’d much appreciate some help with this. Thank you.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public GameObject[] hazards;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
public GUIText highScoreText;
private bool gameOver;
private bool restart;
private int score;
private int highscore;
void Start ()
{
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
highscore = PlayerPrefs.GetInt("High Score");
score = 0;
UpdateScore();
StartCoroutine (SpawnWaves());
}
void Update ()
{
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
SceneManager.LoadScene("Main");
}
}
if (score > highscore)
{
highscore = score;
highScoreText.text = "High Score: " + PlayerPrefs.GetInt("High Score");
print(PlayerPrefs.GetInt("High Score"));
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(UnityEngine.Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
PlayerPrefs.SetInt("High Score", score);
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
highScoreText.text = "High Score: " + score;
//print(PlayerPrefs.GetInt("High Score"));
}
public void GameOver()
{
gameOverText.text = "Game Over";
gameOver = true;
PlayerPrefs.SetInt("High Score", score);
PlayerPrefs.Save();
}
}