- Home /
How to spawn lots of objects without freezing the game
I'm trying to make Minecraft in Unity just for fun and maybe learning stuff along the way, and now I got stuck.
I need to make the chunk generation happen across multiple frames. Is there a way to spawn objects in another thread while the player movement script runs in the main thread?
Have a look at object pooling. It allows you to create the objects you need once and then just re-use them as needed. There are good tutorials on object pooling in Unity Learn.
Answer by rh_galaxy · May 27, 2021 at 07:30 PM
It is possible to spread out some of the generation/loading work to another thread, but in the end all objects need to be added to the main Unity thread.
For example you can generate a mesh and do calculations in a thread, then add it to Unity from the main thread. You will find that there are very specific rules for what you can do from your extra thread, and your code will fail with an error if you run things wrong. It may not be worth the job, since some work must still be made from the main thread, and it will lock Unity up anyway. Also it may be hard to maintain a steady framerate at the same time you are loading stuff.