I am a beginner and i have no idea why it is not saving :/.
Maybe some experts can help me. what is wrong with the code.
Also please give me instructions where i need to add this script, what to connect to eachother ect!
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
[Header("Please Drag Objects Here")]
public Transform player;
public Text currentScoreText;
public Text highScoreText;
private const string highScoreKey = "HighScore";
[Header("Do not change values")]
[SerializeField] private int highScore = 0;
[SerializeField] private int currentScore = 0;
private void start()
{
highScore = PlayerPrefs.GetInt(highScoreKey, 0);
highScoreText.text = highScore.ToString();
}
private 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);
PlayerPrefs.Save();
}
}
private void OnDisable()
{
PlayerPrefs.SetInt(highScoreKey, highScore);
PlayerPrefs.Save();
Debug.Log("I'm being Disabled! The high score is currently: " + highScore);
}
public void Reset()
{
PlayerPrefs.DeleteAll();
highScoreText.text = "unset";
}
}