StaticBatchingUtility why is this not working?

Can anyone see how I’m using StaticBatchingUtility incorrectly below?

I’ve got a 200 x 200 grid (of the same static object) generated at runtime, the draw calls are automatically batched by Unity, all is well.

But if I change or destroy one of those objects, the Batched goes to 0 and the Draw Calls go through the roof. So I was using StaticBatchingUtility as follows, but it’s not working. Batched stays at 0:

...
// Modify the tiles we hit...
base.tileManager.SelectTile(objectHit, true);
GameObject[] allTiles =
 (GameObject[])base.tileManager.allTilesList.ToArray(typeof(GameObject));

StaticBatchingUtility.Combine(allTiles, hexagonPrefab);
...

All of my tiles were created from hexagonPrefab. I thought StaticBatchingUtility would recombine the mesh after I’ve modified/destroyed one of the items within that mesh, but I must be missing something?

Any thoughts/links appreciated

Thanks

So it turns out when I changed the material of the selected object (to show that it was selected), I instanced the material. I needed to reset the renderer to use the original shared material of the root object (in this case hexagonPrefab), otherwise it seems to break the batching completely.

On selection, I do this:

selectedHexagonTile.renderer.material.color = Color.red;
selectedHexagonTile.renderer.material = hexagonPrefab.renderer.sharedMaterial;

Then I call:

StaticBatchingUtility.Combine(allHexagonTiles, hexagonPrefab);

…and it seems to start batching again, and I still see my selected tile.

I don’t understand how that works. If I change the selected object’s material to Color.red (to show that it’s selected), then Unity instances that material. Fair enough. But if I then tell it to use the root hexagon’s sharedMaterial, why does my selected tile still show as red. Wouldn’t that revert it to the original material’s colour (white)?

Confused, but at least things are batching again :slight_smile: