- Home /
Score and HighScore saving script [please help]
Hello i just wanted to ask if anyone knows how to save score when stopping the game i made that when you kill enemy that score is going up by 10 and when you kill and kill it is going up but when i stop the game to edit or try something its going back to 0 can anyone help me how can i make so there is score and high score and then when you die or you stop the game high score is your best run but score resets to 0 like it does
the first picture is from Score Script and second picture is from Enemy script
Answer by zactanpeterson · Apr 22, 2020 at 07:48 PM
ScoreScript.scoreValue += 10 isn't getting called at all, you are completely deleting the object this script is on with Destroy(gameObject), put ScoreScript.scoreValue += 10 above Destroy(gameObject):
if(health <= 0){ Destroy(gameObject); ScoreScript.scoreValue += 10; }
Destroy
will just make sure the gameObject is destroyed at the end of the frame. I don't think it prevents the execution of the following instructions till then. https://docs.unity3d.com/ScriptReference/Object.Destroy.html
Answer by Kwel · Apr 22, 2020 at 08:05 PM
One simple option to save your game high scores is to use PlayerPrefs to save your game state.
For example:
Save (on game over): PlayerPrefs.SetInt("highscore", highScoreValue); PlayerPrefs.Save();
Read (at game start): highScoreValue = PlayerPrefs.GetInt("highscore");
Your answer
Follow this Question
Related Questions
whats wrong with my high score code? 1 Answer
Storing high score 2 Answers
Highscore with points and time 1 Answer
PlayerPref set int and get int 1 Answer
Score and Highscore - Highscore not saving, loading, or displaying. 0 Answers