using System.Collections; using System.Collections.Generic; using UnityEngine; using…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //We need this line for uGUI to work.
public class HighScore : MonoBehaviour { static private Text _UI_TEXT; static private int _SCORE = 0;
private Text txtCom; //txtCom is a reference to this GO’s Text component private MarioController marioController; void Awake() { _UI_TEXT = GetComponent<Text>(); //If the PlayerPrefs HighScore already exists, read it if (PlayerPrefs.HasKey("HighScore")) { SCORE = PlayerPrefs.GetInt("HighScore"); } //Assign the high score to HighScore PlayerPrefs.SetInt("HighScore", SCORE); marioController = FindObjectOfType<MarioController>(); } void Update() { if ((marioController.currentSceneIndex == 0 || marioController.currentSceneIndex == 1 || marioController.currentSceneIndex == 2) && marioController.currentHealth == marioController.maxHealth) { TRY_SET_HIGH_SCORE(marioController.scoreCounter.score); } } static public int SCORE { get { return _SCORE; } private set { _SCORE = value; PlayerPrefs.SetInt("HighScore", value); if (_UI_TEXT != null) { _UI_TEXT.text = "High Score:" + value.ToString("#,0"); } } } static public void TRY_SET_HIGH_SCORE(int scoreToTry) { if (scoreToTry <= SCORE) return; //If scoreToTry is too low, return SCORE = scoreToTry; } //The following code allows you to easily reset the PlayerPrefs HighScore [Tooltip("Check this box to reset the HighScore in PlayerPrefs")] public bool resetHighScoreNow = false; private void OnDrawGizmos() { if (resetHighScoreNow) { resetHighScoreNow = false; PlayerPrefs.SetInt("HighScore", 0); Debug.LogWarning("PlayerPrefs HighScore reset to 0."); } }}
modify this script to match up with this script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class ScoreCounter : MonoBehaviour { public static int SCORE { get; set; } private Text uiText;
private void Start() { uiText = GetComponent<Text>(); LoadScore(); } private void Update() { uiText.text = SCORE.ToString("Score:#,0"); } public void AddScore(int scoreAmount) { SCORE += scoreAmount; SaveScore(); } public void ResetScore() { SCORE = 0; SaveScore(); } private void SaveScore() { PlayerPrefs.SetInt("Score", SCORE); } private void LoadScore() { SCORE = PlayerPrefs.GetInt("Score", 0); } private void OnEnable() { SceneManager.sceneLoaded += OnSceneLoaded; } private void OnDisable() { SceneManager.sceneLoaded -= OnSceneLoaded; } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (scene.buildIndex == 0) // Check if the current scene is Level1 { ResetScore(); } }}
- Assistant
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class HighScore : MonoBehaviour { static private Text _UI_TEXT; static private int _SCORE = 0;
private Text txtCom; //txtCom is a reference to this GO’s Text component private MarioController marioController; private ScoreCounter scoreCounter; void Awake() { _UI_TEXT = GetComponent<Text>(); //If the PlayerPrefs HighScore already exists, read it if (PlayerPrefs.HasKey("HighScore")) { SCORE = PlayerPrefs.GetInt("HighScore"); } //Assign the high score to HighScore PlayerPrefs.SetInt("HighScore", SCORE); marioController = FindObjectOfType<MarioController>(); scoreCounter = FindObjectOfType<ScoreCounter>(); } void Update() { if ((marioController.currentSceneIndex == 0 || marioController.currentSceneIndex == 1 || marioController.currentSceneIndex == 2) && marioController.currentHealth == marioController.maxHealth) { TRY_SET_HIGH_SCORE(scoreCounter.SCORE); } } static public int SCORE { get { return _SCORE; } private set { _SCORE = value; PlayerPrefs.SetInt("HighScore", value); if (_UI_TEXT != null) { _UI_TEXT.text = "High Score: " + value.ToString("#,0"); } } } static public void TRY_SET_HIGH_SCORE(int scoreToTry) { if (scoreToTry <= SCORE) return; //If scoreToTry is too low, return SCORE = scoreToTry; } //The following code allows you to easily reset the PlayerPrefs HighScore [Tooltip("Check this box to reset the HighScore in PlayerPrefs")] public bool resetHighScoreNow = false; private void OnDrawGizmos() { if (resetHighScoreNow) { resetHighScoreNow = false; PlayerPrefs.SetInt("HighScore", 0); Debug.LogWarning("PlayerPrefs HighScore reset to 0."); } }