- Home /
Currency System Not Saving!?
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.
In order to prevent the user from not re-collecting gems if he returns to a level you either have to 1) save the number of gems acquired on each level and randomly place any remaining, or 2) save the state of each gem. If you lean towards the latter solution see the ArrayPrefs2 script in the Unity Wiki for a solution to saving the state of all your gems on each level.
Answer by whydoidoit · Mar 02, 2014 at 06:23 AM
So the best way to do this is to create a unique ID for each gem in a script and have that be auto destroyed if it has already been collected by saving away the ids.
That's pretty easy to do like this, you would add this script to the gems:
OnlyUseOnce.cs
using System.Collections;
using UnityEngine;
[ExecuteInEditMode]
public class OnlyUseOnce : MonoBehaviour {
public string uniqueId;
void Awake() {
uniqueId = string.IsNullOrEmpty(uniqueId) ? System.Guid.NewGuid().ToString() : uniqueId;
if(Application.isPlaying && PlayerPrefs.GetInt("USED_" + uniqueId, 0) == 1) {
Destroy(gameObject);
}
}
public void UseMe() {
PlayerPrefs.SetInt("USED_" + uniqueId, 1);
Destroy(gameObject);
}
}
Now rather than Destroying the gem when the player picks it up you use:
SendMessage("UseMe");
Or
gemObject.GetComponent<OnlyUseOnce>().UseMe();
I like the unique ID system, however it seems Unity cannot save/load booleans as Player Prefs:
12: Error CS0117: 'UnityEngine.PlayerPrefs' does not contain a definition for 'GetBool' (CS0117) (Assembly-CSharp)
18: Error CS0117: 'UnityEngine.PlayerPrefs' does not contain a definition for 'SetBool' (CS0117) (Assembly-CSharp)
I've also tried replacing "GetBool" with "Getbool" and "GetBoolean" just to check it wasn't a spelling error or syntax error, but still no luck. Any idea how to fix this so I can test the unique IDs for the gems?
Ps Thank you for your help thus far!
Your answer
Follow this Question
Related Questions
Save values on different objets with same script? 1 Answer
Drag and Drop PlayerPrefs 2 Answers
How to stop objects you picked up from reappearing when you go to another level and return 1 Answer
How to save different values with same script? 1 Answer
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer