- Home /
how to save a android game
I am making a android game and i have a int variable that i want to be saved if the game is quit so when you start playing again you still have you're highscoe. this is my code:
#pragma strict
static var highscores : int = 0;
function Start () {
} function Update () {
if(highscore.score > highscores){
highscores = highscore.score;
} guiText.text = highscores.ToString();
}
i want the variable: highscores, to be saved.
Answer by fffMalzbier · Feb 25, 2014 at 09:29 AM
Yu can use the PlayerPrefs for that
https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
You could add to your script:
function OnDisable() {
PlayerPrefs.SetInt("Highscore", highscores); //Save Highscore OnDisable
PlayerPrefs.Save();
}
function OnEnable () {
highscores = PlayerPrefs.GetInt("Highscore",0); //Load Highscore OnEnable
}
It works on all platform ,so far i could test it, at least at win/mac/iOS/Android.
the official documentation needs to be updated; it doesn't mention mobile platform at all !
Also, it is recommended to supply 2nd parameter in GetInt()
to set the default value.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134613im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PlayerPrefs don't load again after Android game closes for the first time. 1 Answer
problem with exporting the game to android 0 Answers
How to save a mesh in runtime on android 2 Answers
Enemy locators 1 Answer
Level Button And Quit Button Android 2 Answers