- Home /
Unity Multithreading only works partial
Hey everyone So I tried to implement real simple multithreading.. I start the Thread with following Code in the Start Method:
void StartWorkerThread()
{
ThreadStart threadStart = new ThreadStart(LoopThroughBuildings);
worker = new Thread(threadStart);
worker.Start();
}
But my Thread only seems to run when I select the object where the Thread gets started on. It always works when I use OnGUI -> GUI.Button() ..
Does anyone have an Idea why I have to select the Object in the Editor? And how to work around that..
Sorry but we're missing context here. First of all we assume you want to do this in a Component / $$anonymous$$onoBehaviour script that is attached on a gameobject in the scene?. Next thing is do you test this in playmode in the editor? Or do you want to run the thread at edit time? $$anonymous$$eep in $$anonymous$$d that in the editor it does not automatically stop the thread when you exit playmode. The thread does get stopped forcefully when Unity recompiles the scripts (since the managed environment is shut down) or when you enter playmode. Though you should always take care of gracefully ter$$anonymous$$ating your threads.
Also keep in $$anonymous$$d that most of the Unity API is not thread safe. Furthermore they actively prevent access to most properties of UnityEngine classes from other threads than the main thread. This is because most of these properties are actually mapped to native method calls. This includes properties like ".gameObject", ".transform.position" and even "go.name".
So what exactly are you doing inside your thread? Note that exceptions that are thrown inside your thread are not catched by Unity. So you don't get any error message in the console. If an unhandled exception is thrown inside a thread, that thread simply gets ter$$anonymous$$ated silently.
You already helped me alot :D I really threw a Exception.
I don't want to work with the Unity API in the Thread.. I'm running a Simulation over my not $$anonymous$$onobehaviour classes..
Thanks so much (:
Your answer
![](https://koobas.hobune.stream/wayback/20220612162113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer