0

I am trying to change the Material of wall at run time. I import the model of house from Google Sketchup, which has different materials all in one object (this is shown in the inspector). Whenever I click the next button (>>), it changes the first material of the object. How do I get the references to the other elements? This is what I have so far:

public class Material_GUI : MonoBehaviour {

public Material[] mats;
public GameObject go;
private int index = 0;

// Use this for initialization
void Start () {
    go.renderer.material= mats[index];
}

// Update is called once per frame
void Update () {
}

void OnGUI(){
    GUILayout.BeginArea(new Rect(Screen.width/2-100,Screen.height-60,200,50));
    GUI.Box (new Rect(10,10,190,40),"");

    GUI.Label(new Rect(62,20,100,20),"Wall Testing"+(index +1));

    if(GUI.Button (new Rect(15,15,30,30),"<<")){
        index--;
        if(index<0){
            index = mats.Length - 1;

        }
        go.renderer.material = mats[index];
    }

    if(GUI.Button (new Rect(165,15,30,30),">>")){
        index++;
        if(index > mats.Length -1){
            index = 0;

        }
        go.renderer.material = mats[index];
    }
    GUILayout.EndArea();
}
}
Swadq
  • 1,837
  • 2
  • 15
  • 25
Master143
  • 1
  • 1
  • 2

1 Answers1

0

If you want to change the other material of the renderer, you can use

go.renderer.materials

http://docs.unity3d.com/Documentation/ScriptReference/Renderer-materials.html?from=MeshRenderer

For example:

go.renderer.materials[0] = mats[0];
go.renderer.materials[1] = mats[1];
Klamore74
  • 578
  • 1
  • 6
  • 21