Day 33 of 100 Days of VR: Implementing the High Score System

Step 1: Saving our Data

Step 1.1: Creating our SaveManager

using UnityEngine;public class SaveManager : MonoBehaviour
{
private string _highScoreKey = "highscore";
public void SaveHighScore(float score)
{
PlayerPrefs.SetFloat(_highScoreKey, score);
}
public float LoadHighScore()
{
if (PlayerPrefs.HasKey(_highScoreKey))
{
return PlayerPrefs.GetFloat(_highScoreKey);
}
return 99999999999;
}
}

Variables Used

Walking Through the Code

Step 1.2: Modifying our ScoreManager to Expose Our Score

using System;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public Text Score;
private string _time;
private bool _gameOver;
private float _score;
void Start ()
{
_time = "";
_gameOver = false;
_score = 9999999999;
}
void Update()
{
if (!_gameOver)
{
UpdateTime();
}
}
private void UpdateTime()
{
_score = Time.time;
_time = ScoreManager.GetScoreFormatting(Time.time);
Score.text = _time;
}
public void GameOver()
{
_gameOver = true;
}
public float GetScore()
{
return _score;
}
// we can call this function anywhere we want, we don't need to have an instance of this class
public static string GetScoreFormatting(float time)
{
int minutes = Mathf.FloorToInt(time / 60);
int seconds = Mathf.FloorToInt(time % 60);
float miliseconds = time * 100;
miliseconds = miliseconds % 100;
return string.Format("{0:0}:{1:00}:{2:00}", minutes, seconds, miliseconds);
}
}

New Variables Used

Walking Through the Code

Step 1.3: Use Everything in Our GameManager

using UnityEngine;public class GameManager : MonoBehaviour
{
public Animator GameOverAnimator;
public Animator VictoryAnimator;
private GameObject _player;
private SpawnManager _spawnManager;
private ScoreManager _scoreManager;
private SaveManager _saveManager;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
_spawnManager = GetComponentInChildren<SpawnManager>();
_scoreManager = GetComponent<ScoreManager>();
_saveManager = GetComponent<SaveManager>();
}
public void GameOver()
{
GameOverAnimator.SetBool("IsGameOver", true);
DisableGame();
_spawnManager.DisableAllEnemies();
}
public void Victory()
{
VictoryAnimator.SetBool("IsGameOver", true);
DisableGame();
if (_scoreManager.GetScore() < _saveManager.LoadHighScore())
{
_saveManager.SaveHighScore(_scoreManager.GetScore());
}
}
private void DisableGame()
{
_player.GetComponent<PlayerController>().enabled = false;
_player.GetComponentInChildren<MouseCameraContoller>().enabled = false;
PlayerShootingController shootingController = _player.GetComponentInChildren<PlayerShootingController>();
shootingController.GameOver();
shootingController.enabled = false;
Cursor.lockState = CursorLockMode.None;
_scoreManager.GameOver();
}
}

New Variables Used

Walking Through the Changes

Step 2: Update the Game Over Panels to Show Our Score

Step 2.1: Making Changes to GameOverUIManager

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOverUIManager : MonoBehaviour
{
private Button _button;
private Text _text;
void Start () {
_button = GetComponentInChildren<Button>();
_button.onClick.AddListener(ClickPlayAgain);
_text = GetComponentInChildren<Text>();
}
public void ClickPlayAgain()
{
SceneManager.LoadScene("Main");
}
public void SetHighScoreText(string score, bool didWin)
{
print(_text.text);
if (didWin)
{
_text.text = "You Win! \n" +
"High Score: " + score;
}
else
{
_text.text = "Game Over! \n" +
"High Score: " + score;
}
print(_text.text);
}
}

New Variables Used

Walking Through the Changes

Step 2.2: Calling our GameOverUIManager from GameManager

using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject GameOverPanel;
public GameObject VictoryPanel;
private GameObject _player;
private SpawnManager _spawnManager;
private ScoreManager _scoreManager;
private SaveManager _saveManager;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
_spawnManager = GetComponentInChildren<SpawnManager>();
_scoreManager = GetComponent<ScoreManager>();
_saveManager = GetComponent<SaveManager>();
}
public void GameOver()
{
DisableGame();
_spawnManager.DisableAllEnemies();
ShowPanel(GameOverPanel, false);
}
public void Victory()
{
DisableGame();
if (_scoreManager.GetScore() < _saveManager.LoadHighScore())
{
_saveManager.SaveHighScore(_scoreManager.GetScore());
}
ShowPanel(VictoryPanel, true);
}
private void ShowPanel(GameObject panel, bool didWin)
{
panel.GetComponent<Animator>().SetBool("IsGameOver", true);
panel.GetComponent<GameOverUIManager>().SetHighScoreText(ScoreManager.GetScoreFormatting(_saveManager.LoadHighScore()), didWin);
}
private void DisableGame()
{
_player.GetComponent<PlayerController>().enabled = false;
_player.GetComponentInChildren<MouseCameraContoller>().enabled = false;
PlayerShootingController shootingController = _player.GetComponentInChildren<PlayerShootingController>();
shootingController.GameOver();
shootingController.enabled = false;
Cursor.lockState = CursorLockMode.None;
_scoreManager.GameOver();
}
}

New Variables Used

New Functions Created

Walking Through the Code

Step 2.3: Attaching our Panels back into GameManager

Step 2.4: Fixing our Text UI Display

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store