Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Abacab · Jul 03, 2015 at 09:26 PM · playerprefscloud

How to wait to set PlayerPrefs?

Hello!

I am using an asset that sends all PlayerPrefs data to iCloud once they are updated. The problem is, that when the user for example collects a coin, the game instantly sends the data to iCloud and it creates a 0.5s delay each time it happens, but only once per level (first time).

Is it possible to "store" the playerprefs and then set them when a bool turns true?

Or do you have another solution for me? Thanks a lot in advance.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cherno · Jul 03, 2015 at 10:14 PM

You can use a Coroutine. Pseudocode:

 if(CoinIsPickedUp) {
     StartCoRoutine(MyCoroutine(PlayerPref values you want to save));
 }
 
 IEnumerator MyCoroutine(PlayerPref values you want to save) {
      while(myBool == false) {
          yield return null;
      }
      //Save PlayerPref values you want to save
      yield return null;
 }
Comment
Add comment · Show 6 · Share
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 Abacab · Jul 03, 2015 at 10:23 PM 0
Share

Will this work if the user resets the level?

In other words, will it carry over if the following is called:

 int currentLevel = Application.loadedLevel;
 Application.LoadLevel (currentLevel);


And is it

 if(CoinIsPickedUp) {
      StartCoroutine($$anonymous$$yCoroutine());
  }

avatar image Abacab · Jul 03, 2015 at 10:38 PM 0
Share
 void OnTriggerEnter ()
     {
         if (Coin.gameObject.tag == "Coin") {
 
             if (!collected) {
                 collected = true;
                 CoinController.coins++;
                 Statistics.totalCoinsCollected++;
                 CoinController.coinsCollectedThisLevel++;
                 StartCoroutine(SaveCoinPrefs());
             }
             Destroy (Coin.gameObject);
         }
     }
 
 IEnumerator SaveCoinPrefs() {
         while(CarTouchControls.sendToCloud == false) {
             yield return null;
         }
         PlayerPrefs.SetInt ("Coins", CoinController.coins);
         PlayerPrefs.SetInt ("TotalCoinsEver", Statistics.totalCoinsCollected);
         yield return null;
     }

Does this work? I get build errors for unrelated reasons at the moment so I can' test this.

avatar image Cherno · Jul 03, 2015 at 10:58 PM 0
Share

I passed a parameter to the Coroutine because you wrote about "storing" the prefs before setting them. I see you use the CoinController to store it ins$$anonymous$$d.

The syntax look fine after cursory reading, if the logic is correct I can't say, it depends on how your scripts interact with each other.

avatar image Abacab · Jul 04, 2015 at 09:52 AM 0
Share
 IEnumerator SaveCoinPrefs() {
          while(CarTouchControls.sendToCloud == false) {
              yield return null;
          }
          PlayerPrefs.SetInt ("Coins", CoinController.coins);
          PlayerPrefs.SetInt ("TotalCoinsEver", Statistics.totalCoinsCollected);
          yield return null;
      }

Is it right that both are yield return null;?

avatar image Cherno · Jul 04, 2015 at 09:55 AM 0
Share

Yes, although I am not 100% sure if the second one is necessary. Coroutines have to have a return type, and I think it's the same as with other methods which have a return type: Every code path needs to end wit ha return. You can try commenting-out the second yield return null and see if it throws an error.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

Google Cloud PlayerPrefs Plugin 0 Answers

Cloud recognition in Vuforia 0 Answers

Game/editor stops responding when going to Highscores 0 Answers

PlayerPrefs Player And Enemy Spawn 1 Answer

Save and Retrieve Score not working! 1 Answer


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