Question by
alcohollica · Nov 21, 2015 at 12:20 AM ·
c#playerprefssave
Continue from where you left with PlayerPRefs
Hey guys, I have coin, level, health, hunger variable.I want to save them, I have a code something like that to save coin,
if(coin> PlayerPrefs.GetFloat("coin")){
PlayerPrefs.SetFloat("coin", coin);
}
coin = PlayerPrefs.GetFloat("coin");
but when I buy something, it doesn't decrease.
My hunger decreases by and by but when I exit the game, it starts with %100.
How can I fix them.Thanks in advance!
Comment
Answer by fredz0004 · Nov 24, 2015 at 11:29 AM
Just like you are adding your variable "coin", you also need to decrease it.
coin--; or coin -= 1;
Then you want to do
PlayerPrefs.SetFloat("coin", coin); // to save again
PlayerPrefs doesn't know or care that coin has changed, it only knows what you tell it to save. Alternatively you can do like your code for adding is doing:
if(coin< PlayerPrefs.GetFloat("coin")){ // Notice the < sign
PlayerPrefs.SetFloat("coin", coin);
}
coin = PlayerPrefs.GetFloat("coin");