using System.Collections; using System.Collections.Generic; using UnityEngine; using…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //This line enables use of uGUI classes like Text.
public class ScoreCounter : MonoBehaviour { //Header("Dynamic") public static int SCORE { get; private set; }
private Text uiText; private void Start() { uiText = GetComponent<Text>(); LoadScore(); } private void Update() { uiText.text = SCORE.ToString("Score:#,0"); //This 0 is a zero! } public void AddScore(int scoreAmount) { SCORE += scoreAmount; SaveScore(); } private void SaveScore() { PlayerPrefs.SetInt("Score", SCORE); } private void LoadScore() { SCORE = PlayerPrefs.GetInt("Score", 0); }} make it so the score resets to 0 if the player dies
- Assistant
To reset the score to 0 if the player dies, you can add a public method to the ScoreCounter script that can be called from another script when the player dies. Here's an updated version of the code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreCounter : MonoBehaviour { public static int SCORE { get; private 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); }} In the script that handles player death, you can find the ScoreCounter component and call the ResetScore() method. For example:
- User
- Assistant