Managed trouble with main thread

If you use socket inside your game you probably got some error like :

get_gameObject  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.

I’m ok to debug this, but it’s look it’s not a problem when I run my game in standalone.

Why do you suggest ? Make some specific code for editor, or, always re dispatch socket message/event inside the main-thread ? ( with a List of message read in the Update for example ).

If you are using multi-threading you should not modify unity object that are in the main thread inside a spawned thread, if anything you should delegate back changes to the main thread.

Threads can take objects as custom classes which is enough to do what you need. If you are capable dispatching to the main thread, you should do this. Do not try and modify an object/property from a spawned thread as this will cause a threading exception.

TL;DR - Dispatch or delegate(back to a method) the changes back to the main thread.

I’ve got some info from the unity support :

The Unity API should always be called form the main thread. It can’t be run safely (we don’t support it) from another thread. To workaround this you can write a buffer that stores the things that you want to do in the main thread, which are then processed in the main thread (Update, LateUpdate, etc).

Yes it’s the same for Debug.log. You could try using System.Diagnostics to print the stack trace,