How to change my materials.

Hey all
i was wondering if someone could help me. Basically i have materials or diffuse shader’s on my object if i hit the B key i am telling it to change its colour to blue. Now it works but not all the materials colours are changing. It only changes one material. Here is the script that i made.

var Cabnet : GameObject;




function Update () {

     if(Input.GetKey(KeyCode.B)){
     
     Cabnet.renderer.material.color =  Color.blue;
     Cabnet.renderer.material.color = Color.green;
     
     }

}

Also how do i reference it to the material it has already ? thank you in advance

I think you want to change the color of the materials of all child objects of Cabnet. At the moment you just change the color of the object “Cabnet”. btw.: it doesn’t make sense to change the color to blue and instantly change it to green. I’m not very good at JavaScript since i code in C# but i think that is what you need:

var childs : Renderer[];
childs = CabNet.GetComponentsInChildren (Renderer);
for (var r: Renderer in childs) {
    r.renderer.material.color  = Color.blue;
}

if you would have the script as a component of the cabnet object you could remove the “cabnet” in front of “GetComponentsInChildren”

Good Luck!

//Declare the cabnet color
var CabnetColor = cabnet.renderer.material.color;

/* in the triggering fuction*/

if(Input.GetKey(KeyCode.B))
{
if(CabnetColor = Color.blue)
{
Cabnet.renderer.material.color = Color.green;
}
else
Cabnet.renderer.material.color = Color.blue;
}

//you just need to make it an if else statement right now it changes it quickly to blue then green so it looks like its green. By adding the top variable you should be able to make it refrenece itself.