Save coins/currency?

Hello!

Using player prefs my game stores the currency the player has aquired during his run, when its game over i store the currency:

if (game.gameOver == true && tempsReset == false){
	Debug.Log("Iron collected: " +ironTemp);
	resetTemps = true;
	PlayerPrefs.SetInt ("currencyPref", currency);
}

When i then exit play mode and enter it again i can see the currency from the previous game session in my currency manager:

public int currency;

void Start () {
   currency = PlayerPrefs.GetInt("currencyPref");
}

Until this point everything works fine.

But after the second gamesession the first currency gets overwritten…

If i exit playmode, re-enters and play unitil its game over i want to see the currency gained from ALL the sessions prior to this one. Not only the previous one. How would i achive this?

When you store the currency after a run, you should add the currency earned in this run to the current balance you have read in the Currency Manager.

public int currentBalance;
public int currencyInRun = 0;

void Start() {
     // read the current balance from Player Prefs
     currentBalance = PlayerPrefs.GetInt("currencyPref");
}

void DuringRun() {
     // during run keep incrementing when player earns coins
     currencyInRun++;
}

void OnGameOver() {
    // Do what you want
    // add currency earned to the total balance
    currentBalance += currencyInRun; 
    PlayerPrefs.SetInt("currencyPref", currentBalance);
    currencyInRun = 0; // reset this for the next round
}

Here is a basic idea. You can maintain 2 variables. One that holds the current balance in your bank and the other one holds the currency you have earned in this run. After the game completes, you add the currency in this run to the bank balance and save it in player prefs. Make sure you reset the variable for in round currency before a next round. Hope this helps.

Thanx man it work for me