# My money will only save for one game run

I am trying to make a money system my problem is that the money value only saves for 1 game run then the value returns to 0. Here is my code. What should I do.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Money : variables {

public Text cash;

// Use this for initialization
void Start () {

if (counter < 9 && counter > 1)
{
money = money + 2;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 10 && counter < 20)
{
money = money + 5;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}

else if (counter > 21 && counter < 30)
{
money = money + 15;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 31 && counter < 40)
{
money = money + 25;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 41 && counter < 70)
{
money = money + 50;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 71 && counter < 100)
{
money = money + 75;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 101 && counter < 200)
{
money = money + 100;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);
}
else if (counter > 201)
{
money = money + 150;
PlayerPrefs.GetInt("money", money);
PlayerPrefs.SetInt("money", money);

}

cash.text = "Credits: " + PlayerPrefs.GetInt("money", money);
}
}

• Note that as it is your context highlights other problems in your code. For example, your asking the your logic "how many taps has the player performed" at the start. Quite likely, the answer will always be 0; at the start, the player has performed 0 taps. It might be worth setting a value to money manually, and at least testing your PlayerPrefs that way. There are still some serious issues with the above code; You do not have any reference to money, still. Yeah, we can see its being incremented. But its not being set up. We also do ot see where tapCount comes from.. – Gnemlock Apr 5 '17 at 1:44

In the example code, your setting a value to your PlayerPrefs file under "money" immediately before you retrieve it. In turn, it does save, but your code overrides the value before you retrieve it.

PlayerPrefs.SetInt("money", money);
cash.text = "Credits: " + PlayerPrefs.GetInt("money", money);


No matter what, your not going to notice any form of persistent data, in this way. You set a value just before you retrieve it, and thus, the value you retrieve will always be the value you start from. If you have not set a value to money, it will default as 0. In turn, your setting the "money" value of your PlayerPrefs as 0 immediately before you attempt to retrieve it.

While a lot of your context is still very unclear, you can break your logic into some basic methods. From there, it is only basic organisational logic to ensure you do not call the method to set your value while it is still a default value.

/// <summary>The string used to tag money in the PlayerPrefs.</summary>
/// <remarks>Hard coding a string once ensures we do not make mistakes, later.</summary>
string moneyTag = "money";
/// <summary>The current value for money</summary>
int money;

void Start()
{
money = GetMoney();
}

/// <summary>Retrieves the value of money from the PlayerPrefs</summary>
int GetMoney()
{
// Passing money in as the second value ensures it will be retained,
// should there be no PlayerPrefs value. Otherwise, the default will
// always be 0, voiding inspector-side values.
return PlayerPrefs.GetInt(moneyTag, money);
}

/// <summary>Saves the value of money to the PlayerPrefs</summary>
void SetMoney()
{
PlayerPrefs.SetInt(moneyTag, money);
PlayerPrefs.Save();
}

/// <summary>Increments money by a value according to tapCount, and saves it.</summary>
/// <params name="tapCount">The amount of taps, used to increment money.</params>
void ProcessTapCount(int tapCount)
{
if (tapCount < 9 && tapCount> 1)
{
money = money + 2;
SetMoney();
}
else if (tapCount> 10 && tapCount< 20)
{
money = money + 5;
SetMoney();
}
}


When you want to save the value, you do not retrieve it. When you want to retrieve the value, you do not save it. When you call a Start method, for example, you may want to retrieve the value from the PlayerPrefs file. You would not want to set the value, because we will not have a value for money at the start, unless we actually retrieve it from the PlayerPrefs file.

• So what do you think I should do – jayson meribe Apr 3 '17 at 22:01
• @jayson meribe, whatever you want. You do not give enough information to know how your using money without guesswork. To be honest, if you still dont know, I would recommend following some more Unity tutorials. I wont suggest any, since we do not do that, here. – Gnemlock Apr 3 '17 at 22:29
• @jaysonmeribe, as I have pointed out, that is exactly what the code will do. Your question appears to asks "why does it do that", and so I have answered with "this is why it does that" (looking back, your question does not actually ask a question. That, in itself, is a major red flag). What are you trying to do? How are you actually determining the value of money? When do you actually use the value of money? It is impossible for me to tell you how to solve your problem, because you have not actually told me the problem your trying to solve. – Gnemlock Apr 5 '17 at 0:55
• My game is about tapping, the more taps you get the more money you get ,counter is the value the represents how many taps you get. My problem is that I don't know why or how to make the value save when the application quits and when the application runs. – jayson meribe Apr 5 '17 at 1:03

At game launch, your money variable will always be 0. you should save the pref with the new value at the game end, and retrieve it in the game init part.

int money; // it's surely initialized somewhere

// your Manager in it's Start()