Help making a high score with player prefs/displaying it
I have to make a high score display on the game over and menu scene, Right now I have just once you die, it displayes the score you had gotten from that run and then it deletes it (I assume because the data of the score is not being saved antywhere). I have no idea how to use player prefs but basically, from what I gather, I need to save the scores I get and then make the one with the highest value display. I have no idea how.. :( Here is my score script please help (Java and the name is GlobalScore): static var CurrentScore : int; var InternalScore : int;
var ScoreText : GameObject;
function Update () {
InternalScore = CurrentScore;
ScoreText.GetComponent.<Text>().text = "" + InternalScore;
}
Thanks!
Answer by LucyLuna · Apr 14, 2017 at 04:46 AM
Hope this helps: make an empty gameObject , then add the script below :
public class ScoreManager{
public static ScoreManager instance;
public int highestScore;
void Awake(){
if (instance == null) {
instance = this;
}
if (instance != this) {
Destroy (gameObject);
}
if (PlayerPrefs.HasKey ("score")) {
highgestScore = PlayerPrefs.GetInt ("score");
} else {
highgestScore = 0;
}
DontDestroyOnLoad (gameObject);
}
//it will save the score
public void Save(){
PlayerPrefs.SetInt ("score", highestScore);
}
}
Now , assign the highestScore when you want (i.e gameover)
if(currentScore > ScoreManager.instance.highestScore){
ScoreManager.instance.highestScore = currentScore;
ScoreManager.instance.Save();
}
Cheers. Wink
I made a new c# script, named it Score$$anonymous$$anager. I made a empty gameObject on the gameover scene and attached the script. Im getting the errors: The name "Destory, gameObject, highgestScore, highgestScore (again), DontDestroyOnLoad, and gameObject (again), "does not exist in the current context". And also, I have no idea what to do with the second part of your response, ex: where to put it, etc. Thanks for replying! Please help more Lucy!
Oops, I almost forgot
//without this , everything will be incorrect
public class Score$$anonymous$$anager : $$anonymous$$onoBehavior{
}
and the second part is put where you want to end your game. For example: when your character collide with something that kills him, add the code there. Or when you exit the game ... etc. Hope that helps :)
Your answer
Follow this Question
Related Questions
How do I access playerprefs from another script? 1 Answer
Highscore with player prefs help. (JAVASCRIPT) 0 Answers
Highscore and PlayerPrefs unity C# 1 Answer
Game over high score script 0 Answers
Highscore not working 0 Answers