Here is my Game Controller script. I believe adding a High Score is relatively simple, but for some reason, my code is not working. It won’t even update like a normal score. I’ve tried different extensions on the SetInt and GetInt PlayerPrefs, (,0 ,score ,highScore) but it still did not work. As well as moving my if statement to void update. For more context, this is an extension of Unity’s Space Shooter Tutorial.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public GameObject[] hazards;
public GameObject playerExplosion;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text ScoreText;
public Text RestartText;
public Text GameOverText;
public Text whoText;
public Text highScoreText;
public Text winText;
private bool gameOver;
private bool restart;
private bool win;
private int score;
private int highScore;
void Start()
{
gameOver = false;
restart = false;
win = false;
GameOverText.text = "";
whoText.text = "";
winText.text = "";
highScoreText.text = "High Score: " + highScore;
score = 0;
highScore = PlayerPrefs.GetInt("High Score", score);
UpdateScore();
StartCoroutine(SpawnWaves());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
SceneManager.LoadScene("HardMode");
}
if (Input.GetKeyDown(KeyCode.N))
{
SceneManager.LoadScene("NormalMode");
}
if (restart)
{
if (Input.GetKey("return"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
{
if (Input.GetKey("escape"))
Application.Quit();
}
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[UnityEngine.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)
{
GameOverText.text = "Game Over!";
RestartText.text = "Press 'Enter' to Restart";
whoText.text = "Created By Taylor McNiff";
restart = true;
win = false;
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
ScoreText.text = "Points: " + score;
if (score >= highScore)
{
PlayerPrefs.SetInt("High Score", score);
}
}
public void GameOver()
{
gameOver = true;
}
}