- Home /
Playerprefs and collecting currency
Hello!
I'm having a problem with playerprefs.
I'm trying to add an in-game currency, and while I am able to "collect" the currency, I'm not able to update the amount of currency collected in real-time. It now only adds +1 to my currency when I reset the level (if I have collected >1 currency).
Here is my script. Thanks in advance!
public class CoinTrigger : MonoBehaviour
{
public GameObject Coin;
public int coins = 0;
public Text coinAmount;
// Use this for initialization
void Start ()
{
coins = PlayerPrefs.GetInt ("Coins");
}
// Update is called once per frame
void Update ()
{
transform.Rotate (0, 0, 50 * Time.deltaTime);
}
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
PlayerPrefs.SetInt ("Coins", coins + 1);
Destroy (Coin.gameObject);
coins = PlayerPrefs.GetInt ("Coins");
}
}
void OnGUI ()
{
coinAmount.text = coins.ToString ();
}
}
To which object should I add the script? I now have it on every single coin, which seems stupid but I can't find a better solution.
And it is still not working.
public class CoinTrigger : $$anonymous$$onoBehaviour
{
public GameObject Coin;
public int coins = 0;
public int coinsCollectedThisLevel = 0;
public Text coinAmount;
public Text coinsCollected;
// Use this for initialization
void Start ()
{
coins = PlayerPrefs.GetInt ("Coins");
}
// Update is called once per frame
void Update ()
{
transform.Rotate (0, 0, 50 * Time.deltaTime);
}
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
coinsCollectedThisLevel++;
Destroy (Coin.gameObject);
//coins = PlayerPrefs.GetInt ("Coins");
//coins = coins + coinsCollectedThisLevel;
}
}
void OnGUI ()
{
coinAmount.text = coins.ToString ();
coinsCollected.text = coinsCollectedThisLevel.ToString () + "/9";
}
}
In your original script up there you never increase the number of coins. You only set a playerpref to be equal to coins+1. This does not actually change the value of coins though. Therefore it will always equal the start value of coins plus 1.
In the second version of the script that you posted in that comment there, you increase the value of a coin counting variable but never tell playerprefs to remember it.
So what you have to do is increase the value of coins and then tell playerprefs to remember this new value.
Hang on, I see how you were trying to increase the value of coins. By settings the playerpref and then getting the new value.
Sorry, I didn't see that on my first go. The reason it wasn't working is because you were destroying the game object before getting the new coin value. Once you destroy the gameobject, you destroy all of its components as well, which I imagine includes this script. This means that any code that comes after the destroy will never be executed.
Thanks for the replies. I still have the issue that the number of coins only updates after I reset the level, and it only adds 1 coin even if I collect several.
Also, as I said I have added the script to every individual coin (object). Is there a better way?
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
coins += 1;
coinsCollectedThisLevel += 1;
PlayerPrefs.SetInt ("Coins", coins);
coins = PlayerPrefs.GetInt ("Coins");
Destroy (Coin.gameObject);
}
}
You shouldn't need to say:
coins = PlayerPrefs.GetInt("Coins");
You already increase the value of coins several lines earlier, so this is redundant.
ExtremePowers is right though, where are you saving? At the end of your level, before leaving it or resetting it or whatever you do, try saving.
PlayerPrefs.Save()
Answer by ExtremePowers · Dec 22, 2014 at 10:06 PM
Where are you saving?
PlayerPrefs.Save();
and you need to add 1 to the current value¨
coins += 1;
PlayerPrefs.SetInt ("Coins", coins);
or
coins = PlayerPrefs.GetInt ("Coins");
PlayerPrefs.SetInt("Coins", coins + 1);
It saves my playerpref high scores automatically. Is there really a need to save?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Currency System Not Saving!? 1 Answer
increase playerprefs 1 Answer