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?
Why do you want to see all the currencies gained, why not make a new int and put all the totals on there, so you have an out of game total earned, and your gameover screen gains of the current game? But why show all, say if I play 100 games, where do I keep all of them?:P
Answer by hulahoolgames · Jan 05, 2016 at 10:35 PM
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.
Your answer
Follow this Question
Related Questions
How to reference data between scenes 1 Answer
A Little Help With Player Prefs 0 Answers
How can I save this objects ? 0 Answers
Saving last runs score, only adds the previous score to it. 0 Answers
Application.persistentDataPath 0 Answers