Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

Variable in a DontDestroyOnLoad class resetting.

$
0
0
Hello, I haven't been able to find a specific answer for this. I have a class with DontDestroyOnLoad, and when I "restart" the level, a variable in that class resets. public class ScoreController : MonoBehaviour { public static ScoreController score; public GameObject hs; public float points; public float highscore; void Awake () { if (score == null) { DontDestroyOnLoad (gameObject); score = this; } else if (score != this) { Destroy (gameObject); } } void Start() { score.points = -10; } // Update is called once per frame void Update () { GetComponent ().text = "Score: " + points; hs.GetComponent().text = "Highscore: " + highscore; } } I want "highscore" to persist when I reload the level but it's resets to 0. Anyone knows why? I know I can use PlayerPrefs or even load a binary file but I wanted to use this approach. I call highscore like this ScoreController.score.highscore I don't know if that could be a problem.

Viewing all articles
Browse latest Browse all 416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>