- Home /
GPU instancing breaks when setting RenderQueue through script.
Hi all, i have a custom shader that works perfectly with all the correct Unity Api for GPU instancing and property changes to those instances.
The major problem i am having is when i set a clone’s material’s render order through C# script with,
renderer.Material.renderQueue = 3005; // transparency order
I have noticed Unity will no longer GPU instance duplicates of that clone, meaning “saved by batching” drops to zero and batch count and draw call increment Per duplicate.
If i comment out the renderQueue line from the script, everything returns to normal and all clones using the shader and material are all saved by batching.
Is there a HLSL tag in the shader that i need to modify? If so, what would it look like?
Thanks in advanced.
Answer by Bunny83 · Jul 14, 2019 at 05:33 PM
When you access renderer.material
Unity will automatically duplicate the material for this renderer so this renderer will have it's own seperate instance of the material. Any kind of batching can only happen between objects which use the same material. If you want to change the render queue for all objects that use this material, use renderer.sharedMaterial
. This will not instantiate the material when accessed. This material is the shared material and will affect all objects that use this material (except they have their own material instance).
Beautiful thanks, im positive this is the answer. I have yet to try it!
Your answer
![](https://koobas.hobune.stream/wayback/20220612204840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GPU Instancing and sorting problem 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it possible to modify a material/shader in code? 1 Answer