Combined Total of Different Variable Values

Hi. In my game for every second you survive you get a point. This is your score. You receive a score every time you play. You also have a high score. Now what I want to do is for every point you get you also get a coin. My problem is when I display your money amount on the menu it is just what you score last round. How can I make it so that your money amount is a collective total value of all your scores? Here is the code I am using to display your money amount which is the same as your score:

Saving the money amount:

PlayerPrefs.SetInt ("moneyAmount", score); 

Displaying it:

	private int moneyAmount; 
	public Text moneyamountLabel; 


	// Use this for initialization
	void Start () {

		moneyAmount = PlayerPrefs.GetInt ("moneyAmount");
		moneyamountLabel.text = moneyAmount.ToString(); 
	
	}

Any ideas? Thank you! :slight_smile:

It seems like when you save moneyAmount to PlayerPrefs, you need to add score to the value already stored there.

Something like

int accountBalance = PlayerPrefs.GetInt( "moneyAmount" ) ;
accountBalance += score ;
PlayerPrefs.SetInt( "moneyAmount", accountBalance ) ;

You should check to see if moneyAmount is set, if not set it to score. If it is set, fetch that value and set moneyAmount to that value plus score.