Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Nimdanet · Oct 18, 2016 at 08:50 PM · androidplayerprefsmain thread

GetInt can only be called from the main thread on Android

Hi,

I'm facing some problems recently with Android Build. I have a class to handle save locally, iCloud and Google Snapshot. It works like this:

 public class DataCloudPrefs
 {
         public static void SetInt(string key, int value)
         {
             #if CLOUDDATA_IMPLEMENTED
     
                 #if UNITY_IOS
                 iCloudBinding.setInt( value, key );
                 #elif UNITY_ANDROID
                 snapshotKeys.Add(key, value);
                 #endif
     
             #endif
     
             PlayerPrefs.SetInt(key, value);
             PlayerPrefs.Save();
         }
 
     public static int GetInt(string key)
     {
         #if CLOUDDATA_IMPLEMENTED
 
             #if UNITY_IOS
             return P31Prefs.getInt(key);
             #elif UNITY_ANDROID
             return int.Parse(snapshotKeys[key].ToString());
             #endif
 
         #endif
 
         return PlayerPrefs.GetInt(key);
     }
  }

So, I implemented a rewarded video on my game. On OnComplete function I call, for example:

 GameController.Coins += 100;

which goes something like this:

 public class GameController : MonoBehaviour
 {
    private static int coins = 0;
 
    public static int Coins
    {
       get { return SaveController.Coins; }
       set 
       { 
            coins = value;
            SaveController.Coins = coins; 
       }
    }
 }

 public class SaveController : MonoBehaviour
     {
        public static int Coins
        {
           get { return DataCloudPrefs.GetInt("COINS"); }
           set { DataCloudPrefs.SetInt("COINS", value); }
        }
     }

This runs normally on Unity, on iOS, but on Android it returns me this error:

 10-18 18:48:13.991: E/Unity(24084): GetInt can only be called from the main thread.
 10-18 18:48:13.991: E/Unity(24084): UnityEngine.PlayerPrefs:GetInt(String, Int32)
 10-18 18:48:13.991: E/Unity(24084): UnityEngine.PlayerPrefs:GetInt(String) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/PlayerPrefsBindings.gen.cs:49)
 10-18 18:48:13.991: E/Unity(24084): DataCloudPrefs:GetInt(String) (at /Users/jota/GitHub/OvertimeStudios/CreepyBuster/Assets/Standards/Social Gaming Network/Cloud Data/DataCloudPrefs.cs:242)
 10-18 18:48:14.251: I/Unity(24084): ArgumentException: GetInt can only be called from the main thread.
 10-18 18:48:14.251: I/Unity(24084):   at UnityEngine.PlayerPrefs.GetInt (System.String key) [0x0000a] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/PlayerPrefsBindings.gen.cs:49 
 10-18 18:48:14.251: I/Unity(24084):   at DataCloudPrefs.GetInt (System.String key) [0x00010] in /Users/jota/GitHub/OvertimeStudios/CreepyBuster/Assets/Standards/Social Gaming Network/Cloud Data/DataCloudPrefs.cs:242 

Why is this?

Comment
Add comment · Show 1
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 Nimdanet · Oct 18, 2016 at 10:28 PM 0
Share

I just simplified a bit On OnComplete function I call SaveController.Coins += 100

Save Controller.Coins now get/set directly to PlayerPrefs (ins$$anonymous$$d of my class handling it).

So, I think the problem is when "OnComplete" is called. Does this make sense?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nimdanet · Oct 18, 2016 at 11:15 PM

Solved: AdMob callbacks are not called on Unity main thread

https://github.com/googleads/googleads-mobile-unity/issues/155

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

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

117 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android Game Save and Load doesn't work 0 Answers

[SOLVED] PlayerPrefs work on my PC but not on my Android 1 Answer

PlayerPrefs works fine in Unity Editor but not on Android 0 Answers

Playerprefs being cleared on some devices when the phone is restarted...why!? 2 Answers

I have trouble with Player Prefs. 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