Swapping a texture on a specific material

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.

http://www.youtube.com/watch?v=YLB5t4trs88&feature=youtu.be

I would appreciate any advice or input!

I still haven’t found a solution to what I am trying to do here. :frowning:

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.

https://github.com/technicat/LearnUnity/blob/master/chapter17/unityproject/Assets/Scripts/UVAnim.cs