Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 09, 2015 at 03:29 PM by Abacab for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Abacab · Jul 09, 2015 at 01:35 PM · updateplayerprefs

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!

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Jul 09, 2015 at 02:00 PM 0
Share

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.

avatar image Abacab · Jul 09, 2015 at 02:01 PM 0
Share

It is only updating when it is in the menu, not while in game. :)

avatar image Abacab · Jul 09, 2015 at 02:25 PM 0
Share

This is the coroutine:

 IEnumerator SaveConsumablePrefs() {
         while(CarTouchControls.sendToCloud == false) {
             yield return null;
         }
         PlayerPrefs.SetInt ("Consumables", ConsumableController.consumables);
         yield return null;
     }
avatar image Abacab · Jul 09, 2015 at 02:57 PM 0
Share

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.

avatar image NoseKills · Jul 09, 2015 at 03:08 PM 0
Share

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.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges