I need to enable/disable a very large amount of game objects. Which was is fastest?

Hi,

I have a 3D grid of cubes that can be as 100 ^ 3 (and later on much bigger) and I need to go through them and enable/disable them based on some algorithm but it’s very slow as you imagine.

I tried enable and disable the mesh renderer and box collider but apparently it’s even slower than enable/disable the game object.

I think the final solution is to do it in another thread but it will just gets the lag out of the and will still be very slow.

Thanks

Tried doing it in a co routine and foreach loop?

A

Coroutine, as stated, just makes the game not seem freeze but the time it requires to process those cubes will remain the same.

And how foreach helps? It generates Garbage and is always slower than a normal loop and my problem is not the large amount of cubes, it’s the process that takes time since I can’t not process those cubes but I, potentially, can process them in a way that consumes less time.

Dealing with that many game objects is going to be slow. Do you need them all on screen at the same time. What is it you are wanting to achieve and do they all need colliders?

Don’t. Whatever you are doing, there is a better way then using a million cubes. A million GameObjects will never perform efficiently. Ever.

There are several options depending on what you are doing. Why not describe your goal in more detail for us?

If you are building a MineCraft like system, investigate voxels. Combining the entire simulation into a single Mesh and adding and removing triangles from it will consistently out perform a million cubes.

Another option to look into is particle systems. Particle systems are highly efficient for displaying a large number of similar objects.

You can do the algorithm to determine which cubes to disable on another thread. You must do the actual disabling on the main thread. Unity isn’t thread safe, and will throw a fit if you try and access Unity API from anything other then the main thread.

2 Likes

You are right, voxels should be it.

How long was it taking to enable/disable 1 million gameobjects? Just curious, as I’ve never tried anything close to that.

I’ve changed the project to 2D since it’s for a game jam so it takes a bit to answer your question but actually my current problem is generating them.

Generating 25x25x25 a prefab consisting of a Unity’s own cube with a custom script which is almost empty takes about 12 seconds on my machine (Core i3-2100 @ 3.10Ghz)

Doesn’t surprise me for 15,000 GameObjects. I don’t think the system was ever designed with those numbers in mind. Especially if you have scripts running on them.

If you really want to crash the system, try putting a Debug.Log in Start or Update on that single cube. A physics collider and a rigidbody will also cause a massive slow down.

15,000 objects is a lot to handle.

A lot of them are disable actually and game is actually running fine and I’m still not using any optimization like pooling. I’m sure it is not the way to do it, but just wanted to pinpoint this.

  1. Disable many gameobjects by disabling the root transform in their hierarchy. Construct your hierarchy in a way that you can disable entire groups of gameobjects at once.
  2. Don’t use that many gameobjects. Most efficient way to solve minecraft would be computeShaders with buffers and use OnRenderImage with a Graphics.DrawProcedural() call . This would be 1 drawcall, and you could use the GPU to disable (set size to 0) 15000 gameobjects in no time at all.
    Another problem with 15000 gameobjects is, that Unity automatically tries to batch them into as few drawcalls as possible. This happens on the CPU and becomes more expensive, the more stuff is in your scene.
    Unity also recently implemented Instance Rendering, you might want to check that out.

Take it from someone who has written such a system before. GOs are not the answer.

PS: your title has a typo in it