Fatih Tkale Fatih Tkale - 11 days ago 5
C# Question

PlayerPrefs Not working

Hey so im trying out unitys Playerprefs method and some how it wont save coins and when i close and exit the game it wont save it...

public Text CoinsText;
public int Coins;
public int clicks;


void Start()
{
PlayerPrefs.GetInt("Coins", Coins);
}
void Update()
{
CoinsText.text = "Memes: " + Coins;
if (Input.GetMouseButtonDown(0))
{
PlayerPrefs.SetInt("Coins", Coins);
Coins += clicks;

}
}


}

Answer

You're never assigning to your Coins.

Try this in your Start():

Coins = PlayerPrefs.GetInt("Coins");

Note that the second value is only in case there wouldn't be a value saved.

public static int GetInt(string key, int defaultValue = 0);

Also, don't forget to Save all values before closing the program with:

PlayerPrefs.Save();
Comments