Multiple Materials and Shaders

Hello,

I’ve got 1 model, that contains 2 Materials.

Mesh Renderer shows;
Size: 2
Element 0: Texture1
Element 1: Texture2

Changing a shader using a code would look like this:

function Update()
{
   renderer.material.shader = Shader.Find ("Self-Illumin/Diffuse");
}

Problem is, this does only change the Shader from the First material; Element 0.
Is there a way to be able to change the Shader of the Second material; Element 1, using code?

If so, how? - Thanks in advance!

Under the renderer, you can actually address materials as an array. So you could use materials[0], materials[1], etc.

Hello, thanks for the reply!

After looking around I can’t seem to find out how I could actually do this.

Right now I have this, which does not work (Gives me error)

var materials : Material[1];

function Update () {
   renderer.material.shader = Shader.Find ("Self-Illumin/Diffuse");
}

Well, in that case you have to make sure you have that second material. Does the object have a second material? If it does not then you probably get a null reference error.

Oh, I don’t know much about JavaScript, but I don’t think you need to declare that var up there. Just use renderer.materials[1].shader… to do what you need.

Putting that in Update() is not very prudent, by the way.

Thanks JRavey

I got it to work! :slight_smile: