- Home /
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220612094930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to access additional SharedPreferences on Android 0 Answers
[Android] Save data loss with PlayerPrefs? 2 Answers
Playerprefs don't reset after reinstalling the build on my phone. 1 Answer
Android PlayerPrefs not saving? 0 Answers
I want to use the score I saved in unity in Eclipse to publish to the user's Facebook Feed 0 Answers