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;
codeprivate 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.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 1, 000."); } } - Assistant
Based on the provided code, it seems that the
Update()method in theHighScorescript is only checking if the current scene index is 0. To make the high score update forcurrentSceneIndex1andcurrentSceneIndex2, you need to modify the condition in theUpdate()method.Replace this line of code:
csharpif (marioController.currentSceneIndex == 0 && marioController.currentHealth == marioController.maxHealth)With this line of code:
csharpif ((marioController.currentSceneIndex == 0 || marioController.currentSceneIndex == 1 || marioController.currentSceneIndex == 2) && marioController.currentHealth == marioController.maxHealth)This modification will check if the current scene index is either 0, 1, or 2, and if the current health is equal to the maximum health. If both conditions are met, the high score will be updated.