The profiler does not show instantiation when it happens. But I do know that instantiation is the problem, because it is only happening when a lot of prefabs are being instantiated and it is well known that using instantiation in Unity is a huge job for the CPU.
Before anything is saved in the chunk, like when the player first visits an area objects are first randomly instantiated, but I slow it down by doing a chain reaction of invokes. It would be great if it were possible to do this when loading saved prefabs from a foreach loop as well.
All game objects in my game have data on them that needs to be saved and loaded. Like you say I can’t save and load Game Object data on separate threads. And how would that stop Unity from instantiating an entire list of saved objects at once? The data is saved to a file and loaded from it. It’s very fast at doing that, but the problem here is the CPU intensive “instantiation” of too many objects at once.
What would be great is if there is a way to slow down the foreach loop or use some other way of checking the State s in m_SceneState.m_dynamicObjects one at a time and delaying the next check. Like how I instantiate the objects the first time around using a chain reaction of invokes. But I just don’t know of any way of doing this as an alternative to a foreach loop reading a saved file.
I already activate and deactivate chunks as the player moves around. That was the whole point behind me setting up my game this way. It’s greatly optimised the world, apart from the freezes when instantiating the objects that have been saved in the chunks, it is seamless.
I can’t find the video you mentioned but I don’t work with scenes anyway. I use one main scene for the whole game world. It’s a dynamic procedurally generated destructible world, its not static. Almost everything in the world can be mined/destroyed and new things built. I just have the game world generate the terrain and objects on it as the player moves around and then as the player gets too far away from a chunk, the chunk will save its objects and delete itself. When the player returns to that location a new chunk will be created, it will regenerate a terrain mesh same as before and then it will look up any saved objects that were there last time and instantiate them with whatever data they had on them, like any damage they have taken.
Also scripts cannot find disabled objects in order to enable them. The scripts need to already be aware of them, like added to an array, which is difficult to do in a dynamically generated world like mine. As the world grows and changes, the arrays need to be repopulated. Disabled objects get left out and you end up with a mess of disabled objects getting left behind and the script thinks that chunk is empty so it instantiates new objects and so on. This is why they need to be saved to a file and destroyed and then loaded and instantiated when the player returns to that location.
So I just need a way of slowing down a foreach loop. Or somehow slow down its instantiation of all of the objects it’s just read from the file.