- Home /
Cant get platerprefs to work
I have this script. Everytime i click the attached object, it goes 1+ on a variable. And then loads a scene. But i want this variable "rigtiggodKlik" to be saved.
This is my attempt. But it doesnt work. I could really use some help.
#pragma strict
var rigtiggodKlik = 0;
var klik4:int = PlayerPrefs.GetInt("klik4prefs", 0); // the zero here is the default if the key isn't found.
function Start () {
}
function Update ()
{
PlayerPrefs.SetInt("klik4prefs", rigtiggodKlik);
PlayerPrefs.Save();
Debug.Log(klik4);
}
function OnMouseDown () {
rigtiggodKlik++;
Application.LoadLevel("Takforstemme");
}
Answer by Bunnybomb7670 · Jul 06, 2014 at 02:47 PM
The issue seems to be that when you load it, you save it into a separate variable, this should correctly get your variable on the first frame, however in the update, you are setting it to 0 because the variable 'rigtiggodklik' is defaulted to 0. I suggest that, in your Start function, do something like this :
function Start ()
{
rigtiggodKlik = klik4;
}
This should fix the issue
This is how my script looks now. but it still does not work.
#pragma strict
var rigtiggod$$anonymous$$lik = 0;
var klik4:int = PlayerPrefs.GetInt("klik4prefs", 0); // the zero here is the default if the key isn't found.
function Start () {
rigtiggod$$anonymous$$lik = klik4;
}
function Update ()
{
Debug.Log(klik4);
}
function On$$anonymous$$ouseDown () {
rigtiggod$$anonymous$$lik++;
PlayerPrefs.SetInt("klik4prefs", rigtiggod$$anonymous$$lik);
PlayerPrefs.Save();
Application.LoadLevel("Takforstemme");
}
Answer by Kiwasi · Jul 06, 2014 at 07:55 PM
You need to change your variable names to make readable code.
Then you need to set your intermediate variable to the loaded variable in start, as indicated by @Bunnybomb7670
Then you need to move your saving code out of update and into the on mouse down script. Put it after the increment and before the loadlevel.
I changed the variables so you guys can read it better. English is not my mother language so im having a hard time understanding what to do (some of it). Is it possibly that you/someone just can fix the script, and give it to me? Im not asking for scripts. I just think it would be a little easier to understand it if i have the full script that works.
Answer by thomasindustry · Jul 07, 2014 at 06:02 PM
I'm not sure that you can call PlayerPrefs.GetInt outside of a function. Try moving
var klik4:int = PlayerPrefs.GetInt("klik4prefs", 0); // the zero here is the default if the key isn't found.
inside Start() or Awake()