Accessing other materials than maintexture?

I have an FBX file that has multiple UV’s on it, that has been setup correctly in my project with multiple textures, and I want to swap the images on each of these out during game play… I can change the mainTexture without a problem, but how do I change the other textures on this object? How do I access them programatically… like…

renderer.material.mainTexture

Is there a renderer.material[×].texture? or something similar?

Cheers :-)[/code]

Looks like you want to use sharedMaterials.

–Eric

hmm… This is what the manual says…

Which leaves me back at how to do it using the material collection.

I don’t know if this would work, but have you tried doing the following?

renderer.material = renderer.sharedMaterials[2];
// do changes on renderer.material...

Looking at the docs, it seems that if you use renderer.material, it will create an instance of this material, so it wouldn’t change the sharedMaterials directly, which would change the material for all the objects that use it.

Regards,
Afonso

Remember that sharedMaterials returns an array, so you can set it to use a different array instead. For example, if you had two materials and want to change the colors of those materials for just that object, you can instantiate clones of the materials (which is what Unity does automatically when you change one material anyway). Then you use the new array and change the colors of the cloned materials.

var newmaterial0 = Instantiate(renderer.sharedMaterials[0]);
var newmaterial1 = Instantiate(renderer.sharedMaterials[1]);
var newmaterials = new Material[2];
newmaterials[0] = newmaterial0;
newmaterials[1] = newmaterial1;
renderer.sharedMaterials = newmaterials;
newmaterials[0].color = Color(0,0,0,0);
newmaterials[1].color = Color(1,0,1);

–Eric

Ahh, ok I get it… thanks guys! Much apreciated!