How do you change only one material when there are more than one on an object

I have a model that has multiple materials on it, specifically skin color and eye color.

When I use the code:

go2.renderer.material = myEyeMaterial;

It removes both of the original materials and just adds the eye material, when all I want it to do is replace only the eye material.

How would I choose to change only the eye’s material? I have attached a reference image to help better visualize the issue.

Renderer has a property called materials, which is an array containing all of the materials on one object. So since the eye material is the second one in your case, you may want to try

go2.renderer.materials[1] = myEyeMaterial;

If this is not the correct material try looping through the materials array and check the name or a reference to find the correct one.

var materials = renderer.materials;
materials[1] = eyeMaterial;
renderer.materials = materials

–Eric

Thanks guys. I had actually already tried it Pia’s way, but with no results at all. It wasn’t changing anything about the materials. But doing it with Eric’s syntax, where I temporarily store the materials in a var variable, it will actually work. Can someone please explain to me why this is?