Can objects be dynamically batched again?

Hello folks.

I’m experiencing some problems with dynamic batching. I’ve looked through the questions aswell as the forum pages but I can’t seem to find an answer to my specific problem so I’m turning to you guys.

I have three materials, a red and a green tinted texture and a solid black. The red and green ones are each copied into four shades of green and red respectively into a collection of materials at the start of the game, giving a total of nine materials.

Then I have a prefab (shadows disabled) which I instantiate and dynamically assign a cube-ish mesh. The mesh’s scale is basically randomized in x-, y- and z-axis and should a duplicate be found the previously created mesh of that scale is used. The object is then assigned a random material of the eight colored ones (the black isn’t used here).

This setup creates a number of unique combinations (of meshes and materials) in which copies are neatly batched giving sweet performance. Now to the actual problem, I wan’t to make these cubes black, and furthermore fade them to black. Simply assigning the black material (or any of the existing nine) to the cubes works well, the cube turns black and the batching works fine. But since I want to fade them to black I wrote a shader that does just that over a randomized period of time. This new “fading” material will obviously break the batching but as soon as the fading is over I set the material to the predefined black one but, as you might’ve guessed, the batching is permanently broken.

Does anyone of you have any experience in this area? Or a good explanation to why it won’t work? Any help, pointer or suggestion would be greatly appreciated.

Thanks for your time!

sharedmaterial and sharedtexture. you have to check that any material used on multiple things is referenced to 1 unity reference/instance and not replicated because creating material and the texture normally duplicates and makes a new copy on each time you write it.

Right. So, disabling the duplicate texture and color didn’t solve the issue, but since simply assigning the black color worked with the batching a decided to try another approach.

When I am to shade the cube i instantiate a new copy of the entire cube, adding it as a child to the original cube and disable the original cube’s Renderer. The copy gets a duplicate of the fading material and as the fading is complete I Destroy the copy and re-enable the original Renderer aswell as assigning it the black material.

This approach works, in my case, but it doesn’t really solve the original question and I would really like to have a solution/explanation to that instead. Creating a new GameObject doesn’t feel optimal.