- Home /
Question by
DOMBARR · Aug 03, 2020 at 11:25 AM ·
c#unity 5scripting problem
Why Is My Reset Button for my high score ? Here is my copied script.
using UnityEngine; using UnityEngine.UI;
public class Score : MonoBehaviour {
public Transform player;
public Text currentScoreText;
public Text highScoreText;
public const string highScoreKey = "HighScore";
[Header("Score values")]
[SerializeField] public int highScore = 0;
[SerializeField] public int currentScore = 0;
public void Start()
{
highScore = PlayerPrefs.GetInt(highScoreKey, 0);
highScoreText.text = highScore.ToString();
PlayerPrefs.Save();
PlayerPrefs.DeleteKey("HighScore");
}
public void Update()
{
currentScore = Mathf.RoundToInt(player.position.z);
currentScoreText.text = currentScore.ToString();
if (currentScore > highScore)
{
highScore = currentScore;
highScoreText.text = highScore.ToString();
PlayerPrefs.SetInt(highScoreKey, highScore);
}
}
public void OnDisable()
{
PlayerPrefs.SetInt(highScoreKey, highScore);
PlayerPrefs.Save();
Debug.Log("The high score is currently: " + highScore);
}
public void Reset()
{
PlayerPrefs.DeleteKey("HighScore");
}
}
Comment