- Home /
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
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.
Answer by justinl · Mar 23, 2013 at 05:56 AM
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.
Ok so i have the timer script on my car and have tagged the enemy cars with Finish. I cant figure out how to set the high score time. I'm new to coding so if the solution is really obvious I'm sorry. Here is my new code
var time : int = 1;
var highScoreTime : int;
function Start () {
while(time != 0) {
yield WaitForSeconds(1);
time = time + 1;
}
}
function OnGUI () {
GUI.Box(Rect(0, 0, 100, 30),"Time: " + time);
GUI.Box(Rect(1250, 0, 100, 30),"High Score: " );
}
function OnCollisionEnter(collision : Collision)
{ if (collision.gameObject.tag == "Finish") {set highScoreTime = time;
}
}
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 X$$anonymous$$L 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.
It really depends on how you're "restarting" your game. If you're destroying your player controller car and re-creating one then for sure the score will be lost. If you don't $$anonymous$$d having the variable saved only for the duration of the game, then what I'd do is create a game manager object in your scene. Just a random dummy object and you assign a script to it that will keep track of things like highscore, or how many lives the player has. You will not destroy this object and it will be persistent throughout the life of your program. That's where you can store your highscore. If you want to try using the X$$anonymous$$L thing, then it's really easy because you say it relies on clicking Save, but you can simply do the "click" of the save button in code ins$$anonymous$$d of an actual mouse button click. Just copy and paste what the X$$anonymous$$L example has inside of the if (GUI.Button(_Save,"Save")) { //copy and paste this stuff into the place where you reset the game such as inside the collision.gameObject.tag == "Finish" brackets }
Your answer
Follow this Question
Related Questions
using player time as highscore 1 Answer
Timer highscore trouble 1 Answer
Unity ignoring if statement for high score 2 Answers
How do I make a Highscore for my timer? 1 Answer
"Best Time"/high score help… 1 Answer