Here’s the situation and an example of the problem with my currency system (all in C#). I plan for the player to be able to purchase upgrades between levels if they have enough gems.
Level 1: Player collects 100 gems. (100 gems is saved in PlayerPrefs).
Level 2: Player collects 50 gems. (150 gems saved in PlayerPrefs). So currency DOES save between levels.
However, I have only created 2 levels so far and I have made the player go back to the splash screen at the end of level 2. In which case, when the player reloads Level 1, the gem count is saved and loaded (via PlayerPrefs) but the Gem objects in the level reload - so the player can collect infinite gems. I have set the Gems to be destroyed on touching the Player. Once the gem has been destroyed in-game, I want it to permanently be destroyed (unless the player resets their current save) so the player can only gain the maximum number of gems in any given level.
GEMS:
Here is the Gem script and the relevant excerpt from the Player script.
void OnTriggerEnter(Collider c)
{
if(c.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
PLAYER:
void Awake () {
Load ();
}
void Save ()
{
PlayerPrefs.SetInt("Gems Currency",Gems);
}
void Load () {
Gems = PlayerPrefs.GetInt("Gems Currency");
}
It saves at the end of every level. Hope I’ve made this question clear enough. If you need more information, please let me know and I can make a video to better explain the problem. Hope somebody can help, major sticking point here!
A.C.