Keeping HighScore when restarting level

var _bestTry = 0;
var _bestTime = 0;

function Awake() {
	DontDestroyOnLoad(this);
}

function Update () {
	if(_bestTry == 0) {
		_bestTry = GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).number_of_tries;
	}
	if(GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).number_of_tries <= _bestTry) {
		_bestTry = GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).number_of_tries;
	}

	if(_bestTime == 0) {
		_bestTime = GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).current_time;
	}
	if(GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).current_time <= _bestTime) {
		_bestTime = GameObject.Find("WinorEnd_Controller").GetComponent(script_winOrEnd).current_time;
	}
}

Hey guys, I have this bit of code. It is supposed to keep track of the best score in the scene. However I want this score to be kept when I’m restarting the level. At the moment, I am getting only the latest score. For example, if best try was 1 the first time, and 2 tries the second time, the high score will be 2 tries which is not correct.

I am restarting the level from a different script with Application.LoadLevel(sameLevel);

Thanks in advance!

1 Answer

1

Two methods

1: Use playerPrefs these will maintain even after they close the app down and bring them up, downside is they can be modified.

2: Use a gameObject that is persistant across scenes (use DontDestroyOnLoad(this):wink: Now just check back with that game object at beginning of your scene for high score. Downside will be that when they turn off the game and come back later it will be like starting over.

playerPrefs is awesome! Ty!