Unity3D sub-thread cannot modify the main thread object state

Unable to modify object status after Unity Socket communication

Scenario where the problem occurs

Unity Socket programming, after communicating, the Unity side received a string of JSON characters from the server, and then modified a GameObject property in GameManager.cs based on the JSON value, and then an error was reported. What I modified was a Text text box. the text inside. The error is as follows, which probably means that things in the main game thread cannot be modified in the sub-thread. This is easy to understand, because the Socket communication itself will have a communication process. This is not the main process of Unity but an independent process of C#. process, so something went wrong.

The error message is as follows:

Internal_CloneSingle 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.
UnityEngine.Debug:Log (object)
SocketManagerController:Received () (at Assets/Scripts/SocketManagerController.cs:121)
System.Threading.ThreadHelper:ThreadStart ()

solution

I don't know how it is normally solved this way, because I am not professional in Unity, but I found that the Socket process can modify basic types, such as: string, int, bool.
So my approach is to do it in the Socket thread

Guess you like

Origin blog.csdn.net/lwb314/article/details/128496045