- Home /
I DON'T GET IT :)
playerprefs won't save scores when you close the game
I used playerprefs to save my score between scenes and it works fine but the problem is that if I close the game and run it again my scores are gone .how could I save scores even when I leave the game?
PlayerPrefs is designed to save between games, so if it isn't, then it must be a flaw with your code. Since you haven't posted your code, it will be impossible to help.
it's very simple PlayerPefs.SetInt("SCORES",score);
and in my Score function I used PlayerPefs.GetInt("SCORES");
it works fine the problem is when I leave the game
I agree with @FortisVenaliter, you may use SetInt and GetInt in a way that makes it save a default value before it loads it for example.
Answer by UnityCoach · Aug 04, 2017 at 03:33 PM
Reading your comment makes me think you may do this :
PlayerPefs.SetInt("SCORES",score);
PlayerPefs.GetInt("SCORES");
Note PlayerPefs.GetInt
returns the score, you have to assign it to your variable.
score = PlayerPefs.GetInt("SCORES");
It's my fault, I'll fix it. I'm just copying and pasting, and I usually use serialize rather than PlayerPrefabs. But this is not where you post this answer.
Answer by REDTAPP · Aug 04, 2017 at 03:17 PM
You need to save the score before you quit the application For that there is a especial function https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
void OnApplicationQuit ()
{
//save your score
}
You don't need to save anything if you're using PlayerPrefs.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do you set up a large project? 1 Answer
Leap Motion-Beginner Scripting Issues 1 Answer
How to change the color of an image every two seconds 1 Answer