Im working on a small client/server game where i currently need to load models on the server. I thought this shouldn’t be a big deal because it would basically work the same as on the client:
Player player = ScriptableObject.CreateInstance();
And that should be it. But… I’ve ran into a different problem on the server.
The server is an async multi-threaded socket server. So when a client connects, then the “OnClientConnect()” method is called on the server side and in that method i wanted to instantiate a new Player(). But i’m getting an error (with no other description besides that there was an error…"). My guess is because this instantiating of the new Player() is done on a different thread (the socket thread).
So my question now is, what do i have to do in order to avoid this problem? Can i switch back to the main Unity thread somehow, so i can instantiate my model there…???
I checked it again, and maybe this is even a bug in MonoDevelop?
The bottom line is, is that i want to create an instance of my “Player” class (which inherits from ScriptableObject) in a different thread. This gives me the following error: Error in file: C:/BuildAgent/…/…/MonoManager.cpp at line: 2212.
you can not create or use anything in a different thread that inherits from Unityengine.object, this includes scriptableobject, component and monobehaviour.
unity is not multithread usable nor async usable from other threads, it will simply crash.
all engine scripting has to happen on the same main thread, any communication with other threads has to go through static data structures where you queue information from and to the work threads
Thanks alot for the clarification! I wasn’t sure if this was a Mono bug or not. Guess i’ll have to do some work-arounds for the ‘socket to unity’ communication.
The standard are message queues in both directions and a function you call to update the queues that Unity can touch which you call from within Unity. thats how photon and smartfoxserver work too for example.