Change the material on several objects via script

Hello. I want to change the material on the Lamp, for example. One material is just transparent glass, the second material is this glass with emissive color.

I create a SwitchController, declare public Renderer (because I have LOD Group; read – several objects), declare a couple of public Material, and assign this in Editor.

But it is not working =(

Next code and editor pics.

public class SwitchController : MonoBehaviour {

    public Renderer[] renderers;
    public Material material_a;
    public Material material_b;
    // public Light lightSource;

	// Use this for initialization
	void Start () {

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

    public void Switch()
    {
        Debug.Log("Switch running");

        for (int i = 0; i < renderers.Length; i++)
        {
            if (renderers*.material == material_a)*

{
renderers*.material = material_b;
_
}_
else
_
{_
renderers.material = material_a;
_}*_

}
}
}
P.S. I call this function from GameManager using GetKeyDown. And my Log Message appears in console.
[127871-1.png|127871]

1 Answer

1

if i’m seeing this wright you never call void Switch() ?

No, no. I call this function from GameManager using GetKeyDown. And my Log Message appears in console.

–

well have you tried to use renderer[].sharededmaterial, instead of renderer[].material?

–