- Home /
Question by
Tortelious · May 17, 2013 at 01:46 PM ·
variablespurchaseaddingcoins
Accumulate coins using variables (in-app purchases)
Can anyone help me with the coin calculation. After completing the level I run this code to add the coins to total coins. Totalcoins is used for IAP purposes. However everytime i retry the level the it keeps resetting the total coins value. Thanks in advance for the solution.
if (coins > 0)
{
totalcoins += coins;
PlayerPrefs.SetInt("Total Coins", totalcoins);
//print(PlayerPrefs.GetInt("Total Coins"));
print(totalcoins);
}
Comment
Best Answer
Answer by CHPedersen · May 17, 2013 at 01:59 PM
totalcoins gets reset because its an instance variable of some script you have that gets destroyed and recreated when the level reloads.
For cross-level persistence, do either of the following:
Make the variable static.
Make the variable a member of a script that calls DontDestroyOnLoad(this.gameObject); in its Start or Awake method.
Store the variable in a file on disk.
Store the variable in PlayerPrefs.