Hi! I have a model in a Unity Scene that contains multiple materials on the same mesh. I want to select a specific material, and change that materials main texture in code, but I cannot figure out what the proper C# syntax is that allows me to do that is.
In the past, I have used renderer.material.mainTexture in the case of a gameobject that only contains a single material. In this case, my gameobject has multiple materials and I need to be specific about which material I change the texture of.
The following video describes the situation in better detail.
You can use renderer.Materials to retrieve an array of materials used by the renderer.
If you want to change a specific texture in a material that contains many textures (meaning the shader uses more than one texture), you can use SetTexture.
I have an example on github of how to access and modify one of the materials for a mesh. Specifically, it modifies the UV of the material, so you would replace the call to SetTextureOffset with a call to SetTexture, as mentioned above.