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?
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?
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
Your answer
Follow this Question
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