How do I create a high score system in my game?

My game has you driving around avoiding enemy cars for as long as you can. I want my game to have a score saving system that shows how long you survived. I already have the timer implemented in. The timer script I am using is as follows.

var time : int = 1;

function Start () {
	while(time != 0) {
		yield WaitForSeconds(1);
		time = time + 1;
	}
}

function OnGUI () {
	GUI.Box(Rect(0, 0, 100, 30),"Time: " + time);
}

FTR consider Invoke() or InvokeRepeating() for simple timers in Unity

learn about Player.Prefs

Player.Prefs is also a really good alternative. If it was me I'd probably use that, since you're only saving such small data.

1 Answer

1

simply create another variable called var highScoreTime : int; and then when you die or lose, set highScoreTime = time. Then when the game restarts, don’t overwrite the highScoreTime variable. However, this will be lost as soon as you stop playing the game. If you want it to save it permanently you’ll need to do some extra work such as implementing this XML Save/Load package: http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML or just google “Save/Load in Unity” and you’ll see plenty of various solutions with different degrees of difficulty.

GUI.Box(Rect(1250, 0, 100, 30),"High Score: " + highScoreTime ); highScoreTime = time; //does not require the word "set". What specifically is not working?

Thanks for your help justinl. But I have one more thing I wish to know. I have my game set so that if you get hit by the enemy cars a "you lose" screen comes up and then you go back to the main menu. Once this happens my score disappears . the XML thingy doesn't work as it relies on clicking Save. If you know any alternate ways that would be ideal. if you cant I'll just look at the script and see if i can get ti to trigger on level change or something.