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");
}

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

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’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()