- Home /
Threads for Instantiate
Problem
I start populating my scene with enemies. If I want to instantiate n enemies of type Enemy_A, that takes time that some other subsystem could be doing something.
Idea
I would like to create a thread that instantiates enemies.
Bigger Problem
It seems that Unity won't allow me to call the MonoBehavior
functions like MonoBehavior.Instantiate()
from a thread, even if that thread is independent from Unity. (I got this notion from this Previous Thread Question).
Is there a way around this problem? I thought of using Application.LoadLevelAdditiveAsync
but I would prefer something else. Any suggestions?
Answer by Bunny83 · Nov 11, 2013 at 04:13 PM
Well, if you have actually read the other question you have linked (including the answers), it should be clear that there's no way to use any unity api function from another thread but the main thread. A thread is a thread, no matter if it's a "c# thread" or not. The only way to load something in a background thread is to use unity's pro features like Application.LoadLevelAdditiveAsync. That's it. The API is not thread-safe and there's no way to make it thread-safe.
It was kind of obvious that I read the answers, that is exactly what I am citing above. It might not have been obvious, to you at least, the question and answers are from 2011 and there might be new information on the matter. Thank you though, now I know there is nothing new.
Well, that won't change in the future. The way the api works, they would need to implement tons of locks / semaphores / other synchronizing methods to make everything thread-safe. That would have a horrible impact on performance.
The chance that they change this in any future version is below 1%
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Classes, MonoBehaviours C# 2 Answers
Multiple Cars not working 1 Answer
Using a non-monobehaviour class file 1 Answer
Should I make my own Start / Update? 2 Answers