- Home /
How do I save highscore (Very Simple)
I have the scoring working just fine but when the game restarts it doesn't save the highscore and just resets everything back to 0. I am very new to coding in C# and would very much appreciate the help with getting this code correct so that it saves my highscore. The UI I am using is a text UI not a GUI. Thank you in advanced.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score;
public static int highscore;
Text text;
void Start()
{
text = GetComponent<Text> ();
score = 0;
highscore = PlayerPrefs.GetInt ("highscore", highscore);
}
void Update()
{
if (score > highscore);
highscore = score;
text.text = "" + score;
PlayerPrefs.SetInt ("highscore", highscore);
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
}
public static void Reset()
{
score = 0;
}
}
You can't declare functions inside other functions in C#
(Start and OnDestroy inside Update)
the score is displayed during game up the top left hand corner
Answer by maccabbe · Jul 09, 2015 at 04:31 PM
The code
void Update(){
if (score > highscore);
highscore = score;
text.text = "" + score;
PlayerPrefs.SetInt ("highscore", highscore);
}
is the same as
void Update(){
if (score > highscore){
}
highscore = score;
text.text = "" + score;
PlayerPrefs.SetInt ("highscore", highscore);
}
that is, you're always setting highscore to the current score. Instead you probably want to use brackets as follows
void Update(){
if (score > highscore){
highscore = score;
text.text = "" + score;
PlayerPrefs.SetInt ("highscore", highscore);
}
}
https://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements
You also probably want to set the text.text to highscore in start
void Start()
{
text = GetComponent<Text> ();
score = 0;
highscore = PlayerPrefs.GetInt ("highscore", highscore);
text.text = highscore.ToString();
}
Thank you so much. I have been struggling with this code for too long.
Answer by Ryuzax · May 26, 2016 at 07:57 PM
try using:
void OnDestroy()
{
PlayerPrefs.SetInt ("highscore", highscore);
PlayerPrefs.Save();
}
to actually save the PlayerPref. (hope it helps anyone with similar issues)
Answer by Dave-Carlile · Jul 08, 2015 at 04:34 PM
Where are you displaying the high score?
This line:
text.text = "" + score;
Is only ever displaying the score, and when you reload the game that's going to be 0 at the beginning. If you want it to display the high score then use that variable instead.
Also, to avoid confusion, you should get into the habit of indenting things properly. Instead of:
void Update()
{
if (score > highscore)
highscore = score;
text.text = "" + score; }
Line up the braces...
void Update()
{
if (score > highscore)
{
highscore = score;
}
text.text = "" + score;
}
And your Awake
function is indented too far. That combined with the brace at the end of the line in Update makes it look like you're declaring Awake
inside of Update
. Just a friendly tip on style that will make looking at your code much easier.
See because my game is on a timer that goes for 4 $$anonymous$$utes once the timer hits 0 it auto resets the game and I want to keep my highscore, and that is the only issue I am having everything else works exactly like I intended it to. Just need to keep my highscore. The problem is this part of the code.
void Start() { highscore = PlayerPrefs.GetInt("highscore", 0); }
void OnDestroy() {
PlayerPrefs.SetInt("highscore", highscore);
okay that makes sense but, just to ask quickly how should my code look because I want my highscore to still be there when the player play's the game again?
note: The code works perfect as it is, when you collect the coins it add points works like a charm just need the highscore to be saved so that you don't loose your highscore.
@Omega2077 I've converted your answers to comments - discussion here is done through the comments and Answers are reserved for answers to questions.
Answer by Pavlos_Mavris · May 17, 2020 at 03:47 PM
What if the user delete the game and then download it after 1 month will still be saved the high score?
Your answer
Follow this Question
Related Questions
PlayerPrefs not saving my variable value for HighScore System 3 Answers
PlayerPref set int and get int 1 Answer
Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer
How many different scripts do I need to write to create a highscore system? 1 Answer
Recognising high score when updated with a graphic 0 Answers