- Home /
keep adding to playerprefs
I am making a game similar to something like temple run but like a platform type. I'm 90% finished on my project but i need help with the coin. I know how to use playerprefs to store data but i don't how to add to it. what i mean by this is if the user gets like 300 coin in the first ran, it will display in the main menu. But then if the user gets like 200 in the second run it should add 200 to 300 and in the main menu it should display 500. And so on..
Answer by HenryStrattonFW · Oct 31, 2015 at 12:36 AM
All you have to do for this, is read out the previous value from prefs, add to it, and then write it again. For example.
int lTotalCoins = PlayerPrefs.GetInt("TotalCoins", 0);
lTotalCoins += lCoinsCollectedThisRun;
PlayerPrefs.SetInt("TotalCoins", lTotalCoins);
Thank you for the help, now I can continue :) also this can help me when uses want to buy coins
But I have a problem. The code works fine but when I quit the game and play it again, what ever amount of coins I got the last run it adds to it. For example I have 250 coins. I play the game and I get 20 coins, it adds 20 to 250 so 270 coins. I decide to close the game. Later when I opened it my coin count went up to 290. I close it again and reopen the game, now the coin count is on 310. I can keep opening and closing the game and it will add 20 every time without me even playing.. Any help?
Your answer
Follow this Question
Related Questions
Guide in ArrayPrefs or PlayerPrefx? 1 Answer
DontDestroyOnLoad reset the gameobject 1 Answer
PlayerPrefs Highscore problem 1 Answer
calculate playerprefs and display 1 Answer