How do I get my Quit button to reset the score?
Hi - When I restart the game using the Quit button, it doesn't reset the scores and lives? What am I doing wrong? I included my Gameplay Controller and Game Manager script below. How do I get my Quit button to reset the score? Thank you for any help!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public static GameManager instance;
[HideInInspector]
public bool gameStartedFromTitleScene, gameRestartedAfterPlayerDied;
[HideInInspector]
public int score, coinScore, lifeScore;
void Awake () {
MakeSingleton ();
}
void Start() {
InitializeVariables ();
}
void OnEnable() {
SceneManager.sceneLoaded += LevelFinishedLoading;
}
void Disable() {
SceneManager.sceneLoaded -= LevelFinishedLoading;
}
void LevelFinishedLoading(Scene scene, LoadSceneMode mode) {
if(scene.name == "GameplayScene") {
if(gameRestartedAfterPlayerDied) {
GameplayController.instance.SetScore(score);
GameplayController.instance.SetCoinScore(coinScore);
GameplayController.instance.SetLifeScore(lifeScore);
PlayerScore.scoreCount = score;
PlayerScore.coinCount = coinScore;
PlayerScore.lifeCount = lifeScore;
} else if(gameStartedFromTitleScene) {
PlayerScore.scoreCount = 0;
PlayerScore.coinCount = 0;
PlayerScore.lifeCount = 2;
GameplayController.instance.SetScore(0);
GameplayController.instance.SetCoinScore(0);
GameplayController.instance.SetLifeScore(2);
}
}
}
void InitializeVariables() {
if (!PlayerPrefs.HasKey ("Game Initialized")) {
GamePreferences.SetEasyDifficultyState(1);
GamePreferences.SetEasyDifficultyCoinScore(0);
GamePreferences.SetEasyDifficultyHighscore(0);
GamePreferences.SetMediumDifficultyState(0);
GamePreferences.SetMediumDifficultyCoinScore(0);
GamePreferences.SetMediumDifficultyHighscore(0);
GamePreferences.SetHardDifficultyState(0);
GamePreferences.SetMediumDifficultyCoinScore(0);
GamePreferences.SetHardDifficultyHighscore(0);
GamePreferences.SetMusicState(0);
PlayerPrefs.SetInt("Game Initialized", 123);
}
}
void MakeSingleton() {
if(instance != null) {
Destroy(gameObject);
} else {
instance = this;
DontDestroyOnLoad(gameObject);
}
}
public void CheckGameStatus(int score, int coinScore, int lifeScore) {
if(lifeScore < 0) {
if (GamePreferences.GetEasyDifficultyState() == 1) {
int highScore = GamePreferences.GetEasyDifficultyHighscore();
int coinHighScore = GamePreferences.GetEasyDifficultyCoinScore();
if (highScore < score)
GamePreferences.SetEasyDifficultyHighscore(score);
if (coinHighScore < coinScore)
GamePreferences.SetEasyDifficultyCoinScore(coinScore);
}
if (GamePreferences.GetMediumDifficultyState() == 1) {
int highScore = GamePreferences.GetMediumDifficultyHighscore();
int coinHighScore = GamePreferences.GetMediumDifficultyCoinScore();
if (highScore < score)
GamePreferences.SetMediumDifficultyHighscore(score);
if (coinHighScore < coinScore)
GamePreferences.SetMediumDifficultyCoinScore(coinScore);
}
if (GamePreferences.GetHardDifficultyState() == 1) {
int highScore = GamePreferences.GetHardDifficultyHighscore();
int coinHighScore = GamePreferences.GetHardDifficultyCoinScore();
if (highScore < score)
GamePreferences.SetHardDifficultyHighscore(score);
if (coinHighScore < coinScore)
GamePreferences.SetHardDifficultyCoinScore(coinScore);
}
gameStartedFromTitleScene = false;
gameRestartedAfterPlayerDied = false;
GameplayController.instance.GameOverShowPanel();
} else {
this.score = score;
this.coinScore = coinScore;
this.lifeScore = lifeScore;
// GameplayController.instance.SetScore(score);
// GameplayController.instance.SetCoinScore(coinScore);
// GameplayController.instance.SetLifeScore(lifeScore);
gameStartedFromTitleScene = false;
gameRestartedAfterPlayerDied = true;
GameplayController.instance.PlayerDiedRestartTheGame();
}
}
} // GameManager
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameplayController : MonoBehaviour {
public static GameplayController instance;
private AudioSource audioManager;
// [SerializeField]
// private AudioClip deadClip;
[SerializeField]
private Text scoreText, coinText, lifeText;
[SerializeField]
private GameObject pausePanel, gameOverPanel;
[HideInInspector]
public bool countScore;
void Awake () {
MakeInstance ();
Time.timeScale = 0f;
audioManager = GetComponent<AudioSource> ();
}
void Start() {
}
void MakeInstance() {
if(instance == null) {
instance = this;
}
}
public void GameOverShowPanel() {
gameOverPanel.SetActive (true);
// audioManager.clip = deadClip;
// audioManager.Play ();
}
public void PlayerDiedRestartTheGame() {
StartCoroutine(PlayerDiedRestart ());
}
IEnumerator PlayerDiedRestart() {
yield return new WaitForSeconds (1f);
// SceneManager.LoadScene ("GameplayScene");
SceneFader.instance.LoadLevel ("GameplayScene");
}
public void SetScore (int score) {
scoreText.text = " " + score;
}
public void SetCoinScore (int coinScore) {
coinText.text = " " + coinScore;
}
public void SetLifeScore (int lifeScore) {
lifeText.text = " " + lifeScore;
}
public void PauseTheGame() {
Time.timeScale = 0f;
pausePanel.SetActive (true);
}
public void ResumeGame() {
Time.timeScale = 1f;
pausePanel.SetActive (false);
}
// public void quit() {
// Debug.Log ("has quit game");
// Application.Quit;
// }
public void ReStartGame() {
Time.timeScale = 1f;
PlayerScore.coinCount = 0;
PlayerScore.scoreCount = 0;
PlayerScore.lifeCount = 2;
GameplayController.instance.SetScore(0);
GameplayController.instance.SetLifeScore(2);
GameplayController.instance.SetCoinScore(0);
SceneFader.instance.LoadLevel ("TitleScene");
}
} //GameplayController
Comment