make it so this script: using System.Collections; using System.Collections.Generic;…
- User
make it so this script: 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 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); } 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", 1000); Debug.LogWarning("PlayerPrefs HighScore reset to 1,000."); } }} references 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;
code - Assistant
To make the first script reference the second script, you can add a reference to the
ScoreCounterclass at the top of theHighScorescript. Here's the updatedHighScorescript:csharp - User
the high score is not updating along with the score
- Assistant
- User
there is no SCORE_CHANGED event in the ScoreCounter script
- Assistant
Apologies for the confusion. In that case, we can modify the
ScoreCounterscript to add a delegate and event for score changes. Here's the updatedScoreCounterscript:csharp - User
scoreCounter still does not contain a definition for SCORE_CHANGED
- Assistant
- User
why is it giving this error: NullReferenceException: Object reference not set to an instance of an object ScoreCounter.UpdateUI () (at Assets/__Scripts/ScoreCounter.cs:25) ScoreCounter.ResetScore () (at Assets/__Scripts/ScoreCounter.cs:40) ScoreCounter.OnSceneLoaded (UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) (at Assets/__Scripts/ScoreCounter.cs:62) UnityEngine.SceneManagement.SceneManager.Internal_SceneLoaded (UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) (at <913bb12059fd4cef8da5cc94ad2f0933>:0)
- Assistant
The
NullReferenceExceptionerror is occurring because theuiTextvariable is not assigned in theScoreCounterscript when theUpdateUImethod is called. To fix this, you can assign theuiTextvariable in theStartmethod of theScoreCounterscript. Here's the corrected code:csharp