- Home /
ThreadCheck error in socket programming and instantiate a object!!!
Hi,
i wrote a server/client program.i send some random numbers(x,y) via server to client and when i get this numbers in client(unity) i want instantiate a bullet in this numbers(x,y) that i get.i can do all of thats but when i instantiate the bullet the unity will crach and sometimes i get this error :
m_ThreadCheck && !Thread::Equals Current Thread ID(m_ThreadID)
in addition,when i build the program in unity i do not get the error but it is not run as like i want.
Project Link:http://www.multiupload.com/7EW8KQNJ3N
Answer by StephanK · Jun 14, 2010 at 07:18 AM
Unity is not Thread-safe. I guess you have a thread running, that checks for input from the socket and that instantiates the bullet. This won't work, as Unity can only instantiate objects during certain times in the game loop. You can't know when the message arrives and if it does during a time other than update time unity will probably crash. To fix this you have to put all code that may change unitys state (especially the renderstate) into one of the Update functions. So you will have to either check the socket from within the update function, or if you need to run it on its own thread don't interact with the engine directly, but rather take a note (set a bool) when a message arrived and deal with it on the next update.
void Update() { if (needsUpdate) { // instantiate/update objects here needsUpdate = false; }
void SocketThread() { string msg = socket.Read(); if (msg != "") needsUpdate = true; }
The part where the socket is read is pseudo-code and probably won't compile, but you should get the idea.
Your answer
Follow this Question
Related Questions
Threaded Socket Client with Thread-Safe Queue for Unity 0 Answers
Thread pool or socket bug? please help,,, 0 Answers
Is it possible to query the Unity (3.0) transform tree from a secondary thread? 1 Answer
Socket continues to operate even after stopping the run 1 Answer
In edit mode SocketServer thread will be stopped when any asset change 1 Answer