Playerprefs not work as expected

Hi folks, i want some help with Playerprefs. I want to save the player’s cash, so if he close the game, when he open again the cash values should be the same. I made this function;

void Save(){

		PlayerPrefs.SetInt ("Cash", coins_print);

		Debug.Log ("save"+coins_print);


and in the Start() function i have:

coins_print = PlayerPrefs.GetInt ("Cash");

I call Save() funcion every time the cash value changes, is that ok?

but this seems to not be working. It is suposed to work in unity or this works only if i build the game?

Thanks for attention.

Hi @miksams

It should work on both unity editor and builded game. Your question is not clear enough. What is printed on the console when debug line is executed? It seems you are not adding on your “Cash” prefs. Whenever you collect cash, you should increment your “Cash” prefs. See the code below:

void Save(int cashCollected){

PlayerPrefs.SetInt("Cash" , (PlayerPrefs.GetInt("Cash") + cashCollected));

when you collect a cash, call “Save” method passing cash value parameter to it.

I hope I got your question true and I hope this answers is helpfull :slight_smile:

@Ozan_2 hi, the coins_print is the total cash, i add 1 coin every X seconds to coins_print and every time i call Save() function and i tought i was making it right. And i made Save() funcion without parameter because i am a new to programming and is a little difficult to me to understand.