why isn't my high score script working?
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;
}
}
Answer by highpockets · Apr 22, 2019 at 09:48 PM
In the Start() method the following should be moved to the last line and you should change the highScore int to a string:
highScoreText.text = "High Score: " + highScore.ToString();
This is because you haven’t set the highScore to any value where you have it, so it will have a value of 0 and the text property takes a string, not an int.
Other than that, I don’t see where you call AddScore(), but I don’t see anything else that stands out. Let me know if the above works.
Cheers
Your answer
Follow this Question
Related Questions
how do i store highscore locally C# (SIMPLE) 3 Answers
Hey i am trying to add a time score system and time high score system need help. 0 Answers
A way to move "Highscore" Text to Game Over Screen? 1 Answer
High Score Save iOS 0 Answers
GUI Highscore? 0 Answers