Very long delay upon starting the game

I have a simple test project that creates a lot (like tens of thousands) of objects via Instantiate, in the Awake function of one of my scripts. The objects are simple cubes —*textured, but no scripts on them (and I tried it both with and without a collider; no significant difference).

Here’s the odd thing: the umpteen cubes appear in the GUI almost instantly (within a second or two), but the game itself doesn’t actually start until a couple minutes (!) later.

So I ran Shark on it, in hopes of getting a clue what it’s doing for all that time — I’m hoping that with understanding, I will find some way to optimize around the problem. It turns out that 97% of that time is being spent in SceneTracker::TransformHierarchyChanged — and much of the time within that is in allocating C++ strings (with a smaller portion being spent in SemiNumericCompare).

But at this point I’m not much enlightened. Anybody know what this method does, why it feels a need to allocate so many strings, and how I can make it stop? When instantiating a bunch of objects at once, is there any way to tell Unity not to recompute stuff until I’m all done?

Thanks,

  • Joe

The number of objects is ultimately likely to be the problem - 10000+ is a lot for one scene. If you have many repetitions of essentially the same mesh (for displaying a molecule, say) then you might find Graphics.DrawMesh more efficient. This allows a mesh to be drawn repeatedly without the overhead of creating a GameObject for each one. This is useful in cases where the individual objects don’t have independent movement, scripting, parenting, etc.