How to change a specific material when the mesh has more than one material?


I'm using the WWW class to change the mesh material texture. My problem is that my mesh contains more than one material (elements 0 and 1) and when I'm applying a "renderer.material.mainTexture", it only changes the "element 0" material texture. But I want it to change the "element 1" material. Can anyone help on how to go about doing this?

***Here is the script I'm using:

// Get the latest webcam shot from outside "Friday's" in Times Square
var url = "";
function Start () {
// Start a download of the given URL
var www : WWW = new WWW (url);

// Wait for download to complete
yield www;

// assign texture

renderer.material.mainTexture = www.texture; 

You have to change renderer.materials, rather than renderer.material.

var mats = renderer.materials;
mats[1].mainTexture = www.texture;
renderer.materials = mats;

If you want to change all material elements of object’s children than here is the answer: How to change all material elements of object's children - Questions & Answers - Unity Discussions