- Home /
How to save High Scores
Hey guys Im stumped on how to save high scores. After my player gets killed and he has the highest score I want his score to be saved. I know I use PlayerPref class but im not sure how to use this.
static var score = 0; //current score
var totalScore = 0; //combined total of players score
var highscore = 0; //highest score player has gotten
var scoretext : GUIText;
var money : AudioClip;
function Update()
{
scoretext.text = score + ""; //display score
if(score > highscore && TurnGameOver.dead2 == true) //when player dies set highscore = to that score
{
highscore = score;
PlayerPrefs.SetInt("High Score", highscore);
Debug.Log("High Score is " + highscore );
}
}
function OnTriggerEnter(other : Collider)
{
//add to players score if he collects a gem
if(other.gameObject.name == "GreenGem" || other.gameObject.name == "BlueGem" || other.gameObject.name == "RedGem" ||
other.gameObject.name == "GreenGem(Clone)" || other.gameObject.name == "BlueGem(Clone)" || other.gameObject.name == "RedGem(Clone)")
{
score += 10;
Debug.Log("Your score is " + score);
Destroy(other.gameObject); //Destroys Gem after player collects it
}
}
Have you read the Documentation? Or one of the many other questions here on using PlayerPrefs? Or done any searching?
You havn't typecast your score variables so I'm going to assume they are integers :
Well, you are saving it. What is your problem? Are you getting any errors? Is the score not appearing anymore?
Why with the attitude alucardj? Geez. Don't you think people have looked and searched before? Either help the guy out or STFU. There si too much unity superiority here. I have the same questions as this guy does and it gets old reading dozens of these comments.
@wheretheidivides : Check the links in my comments, then pull your head in. You don't know good help when you're getting it.
Answer by Seth-Bergman · Dec 14, 2013 at 06:02 AM
you want to save the high score so it still shows up after closing and relaunching the game, yes? you have done the first part right, saving the score.. but in order to have it show up next time, you need to retrieve it first.. use the Start function:
function Start(){
highscore = PlayerPrefs.GetInt("High Score");
}
simple as that!
Im a little late on the reply but thanks a ton man, your the best xD