- Home /
The question is answered, right answer was accepted
Real-Time Consumable tracking
Hello!
I can't for the life of it figure out how to make my consumables update on button press. I am setting the PlayerPref later since it is causing lag if I set it right away (iCloud).
This is my consumable class:
public class ConsumableController : MonoBehaviour
{
public static int consumables = 0;
public Text consumableAmount;
public static bool inMenu = false;
void Awake ()
{
consumables = PlayerPrefs.GetInt ("Consumables");
}
void Start () {
inMenu = false;
}
void Update ()
{
if (inMenu) {
consumables = PlayerPrefs.GetInt ("Consumables");
}
consumableAmount.text = consumables.ToString ();
}
}
And this is the consumable button:
public void ConsumableButton ()
{
if (PlayerPrefs.GetInt ("Consumables") >= 1) {
ConsumableController.consumables--;
StartCoroutine(SaveConsumablePrefs());
}
ConsumableAmount.text doesn't update when I press the consumable button. It only updates once I set the playerprefs in the coroutine.
Thanks a lot in advance!
Could it be that when you decrement consumables--
, the old value gets loaded from prefs in Update before your saving corutine saves the decremented variable?
It looks strange that you are constantly loading from prefs... shouldn't you just trust the variable consumables
and only load it once in the beginning and save it when it changes.
It is only updating when it is in the menu, not while in game. :)
This is the coroutine:
IEnumerator SaveConsumablePrefs() {
while(CarTouchControls.sendToCloud == false) {
yield return null;
}
PlayerPrefs.SetInt ("Consumables", ConsumableController.consumables);
yield return null;
}
If I add print (onDemandTurbos); To the Update-function in ConsumableController it changes from 10 to 9 when pressing the consumablebutton, but the ConsumableAmount.text doesn't update.
You could make consumables
a property, only use that ins$$anonymous$$d of the field, and add Debug.Log in the setter to see what is changing it and when... or use debugger and breakpoints.
Follow this Question
Related Questions
PlayerPrefs Deleted when Updating Game (Windows Phone) 1 Answer
Updating ui not working in fixed update .But it works in update 1 Answer
do player preferences get deleted if an iphone app is updated 1 Answer
Boolean Trouble, Please help....Boolean does not flip another boolean 0 Answers
GUI.TextField problem, won't update var. 0 Answers