- Home /
How do i make a variable that wont change when i re-open the app
all information needed is above.
For a lot of Qs, if you just try to think of another way to ask it, you can Search and find an already written answer.
$$anonymous$$eeping data between runs is known as "Saving." So look up how to save data in Unity (playerPrefs, mentioned below, is one way.)
Of course, many tablet/phone Apps don't really quit when you close them, so it works a little funny.
Answer by Marco-I · May 12, 2015 at 01:10 PM
I'm supposing that you're referring to a "STATIC" variable, i think?
Nop, he wants a variable that'd be persistent between to runs of the application.
Tip: When you just ask a question and not give an answer, prefer using the comments ;)
Answer by Dblfstr · May 12, 2015 at 06:07 PM
PlyerPrefs is the way to go http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
int myVariable;
string myVariableKey = "MyVariable";
void Start(){
// Get your saved variable and set it to myVariable
myVariable = PlayerPrefs.GetInt(myVariableKey ,0);
}
void Update(){
//Do something with myVariable;
myVariable += 1;
}
void OnDisable(){
//Store your new varaible in Player Prefs under the key myVariableKey
PlayerPrefs.SetInt(myVariableKey, myVariable);
//Save your Player Prefs
PlayerPrefs.Save();
}
You can choose to SetInt() some other place as well, but make sure to Save(); before you quit.
Your answer
