- Home /
[Problem] Use Playerpref.GetInt in other thread
Hi! i'm having a problem using playerpref.getint in other thread created in the game. i have been trying ways of us playpref.getint in other thread for more than 8 hours.
i'm doing this:
Thread t = new Thread(ScoresOnline);
t.Start();
and this:
void ScoresOnline(){
int bestSctore = PlayerPrefs.GetInt("Score")
scoreBoard.SaveScore(bestSctore);
(...)
}
Im getting this error:
GetInt can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Please anything that could help, would be great, i have already tried a lot of things, but not the right one. So, can you help me?
You can't. End of story. Unity's backend requires a lot of elements to be synchronised under the hood. The easiest way to ensure this happens is to prevent multithreading at all. What you can do, though, is use the standard .NET IO libraries, ins$$anonymous$$d of Unity's PlayerPrefs.