- Home /
Modify a material at runtime
I want to modify a material's parameter at runtime. The parameter is called "Radio 1" and is defined in a custom shader. I need the change I make to the material to affect all objects that are using it, and that's not working. How can I do so?
I've tried getting one of the objects that use the material and modify the sharedMaterial, and also tried loading the material and modifying it like this:
var mater = Resources.Load("BGRingMat") as Material;
mater.SetFloat("Radio2", bgRingMaterialRadio2);
I see no effects at all. How can I achieve this?
If you made any changes in the materials of the objects...changes the color, changed the blend mode, replaced the texture...then each object will have its own material instance (and therefore will not reflect a change to the shared material). In addition, its not clear to me that you can get access to the shared material using the mechanism you outline. Have you tried with Renderer.shared$$anonymous$$aterial? Though as I think about it, there may be ramifications to using this property.
@robertu. Yes, I've used Renderer.shared$$anonymous$$aterial but had no luck. I also want all the meshes to be batched together, and if I modify each mesh material then Unity won't be able to batch them, so modifying the material looks like the way to go, but I can't find the way to do so.