- Home /
Is the Unity API threadsafe?
I notice that Unity supports background loading of asset bundles, so I guess uses threads. Can I use threads in my own script and plugins? Are there any issues to be aware of?
Answer by Lucas Meijer 1 · Oct 18, 2009 at 01:42 PM
No.
Every call you make into the Unity API, must be done from your main thread. It is possible for you to create your own threads, to make whatever calculations you like, however, the additional threads you create yourself should not call any Unity functions. They should communicate their results back to the mainthread, which can then call the Unity functions.
If you try to do this anyway, it might accidentally work, however future Unity updates (including those of the webplayer) are very likely to make this break.
To help you avoiding using the Unity APIs in a non-threadsafe manner, Unity 2.6 will now throw an exception in the editor if your code invokes API functions from a thread that's not the main thread.
For performance reasons this check is disabled in published builds however.
Hey $$anonymous$$eli, thanks for that information (and also thanks to whoever implemented that) - sounds like a great idea to me to make Unity more robust!
the assert you get is:
m_ThreadCheck && !Thread::EqualsCurrentThreadID(m_ThreadID)
I have been running into this issue, without explicitly starting a new thread. The one thing I suspect is the root of this evil is my usage of a static class, my lazy way of keeping track of my "globals". When trying to asyncloadlevel from my static class i get the dreaded m_ThreadCheck message
Answer by $$anonymous$$ · Nov 07, 2009 at 09:48 PM
You can achieve some of the advantages of threads by using coroutines - see the Coroutines and Yield overview in the Scripting Reference.
this question is asked and answered by unity technologies staff to $$anonymous$$ch us something. someone that wants to use threads already knows coroutines. :D threads are some of the most advanced stuff in program$$anonymous$$g. however using them in C# is alot easier than C++. unity's scripting engine is the best choice that they made! thank you guys
Your answer
![](https://koobas.hobune.stream/wayback/20220613154517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Is it okay to mark well-known events as private? 1 Answer
play animation sequence 3 Answers
What are the benefits of using Invoke over Coroutine or vice versa? 1 Answer
WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer