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 ();
}
}