- Home /
Do Asynchronous GameObject.Instantiate Exists?
Hi! I'm making a procedural game, and I've made a system that Instantiates/Destroys GameObjects, to avoid lag, based on distance.
The only problem is that some of the prefabs that needs to be spawned are complex, and when they spawn/destroy they cause annoying frame-rate drops.
Is there a way to do a sort of Asynchronous GameObject.Instantiate, so the frame-rate drops are less annoying? (Or if you know another way to avoid this problem)
What you are looking for is called Object Pooling, Unity has provided a tutorial on the subject HERE
Answer by MrMeows · Oct 25, 2015 at 04:44 AM
Use SetActive instead of instantiating and destroying. That way, the GameObjects remain in the scene but require no processing.
I've tried that before, but the game lags. It's probably because meshes are still processed by the engine, just not rendered.
For lag I mean 1-2 fps. (unfortunately it's more than 800 objects)
I can think of no reason why anything would be processed. I am suspicious that the active GameObjects are enough to cause the lag. Run the profiler (Ctrl+7) to find the cause. If it actually is rendering, you can reduce your quality settings. However, I usually have problems with OnTriggerStays, and there are few, if any, ways to alleviate that.
Answer by josh-carberry · Oct 30, 2021 at 08:44 AM
Use the unity.addressable package https://docs.unity3d.com/Packages/com.unity.addressables@0.4/manual/index.html
you can find a function called InstantiateAsync
public async Task LoadAddress(string path)
{
AsyncOperationHandle<GameObject> op = Addressables.InstantiateAsync(path);
op.Completed += OnLoadDone;
await op.Task;
}
void OnLoadDone(AsyncOperationHandle<GameObject> op)
{
GameObject obj = op.Result;
//Do stuff
}
You can then set that up in an async thread and await op.task you can add functions to the op.Completed Action and they will run when it is complete. To load them all in at once you can skip the await but i wouldn't advise that for large files.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042542im_/https://answers.unity.com/themes/thub/images/avi.jpg)