Changing material on click

Hi, i’ve been trying to change the material of an object when i click but it isn’t changing… i’m starting to learn c# , can someone help me??

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interruptor : MonoBehaviour {

public Light Luz;
public Material Luzacesa;
public Material Luzapagada;
public GameObject Lampada;

void OnTriggerStay (Collider other) {

	if (Input.GetKeyDown (KeyCode.Mouse0)) {
		Luz.enabled = !Luz.enabled;
		if (Lampada.GetComponent<Renderer> ().material = Luzacesa) {
			Lampada.GetComponent<Renderer> ().material = Luzapagada;
		}

		else if(Lampada.GetComponent<Renderer> ().material = Luzapagada){
			Lampada.GetComponent<Renderer> ().material = Luzacesa;
		}

	}

}

}

Hello,

I think your code is a little too complicated for what your are trying to do. The most the code is short, the better it’s working.

using UnityEngine;

public class Clicker : MonoBehaviour 
{
	public Material mat1;
	public Material mat2;

	private bool _isMat1;
	private Renderer _yourObjectRenderer;

	void Start()
	{
		_yourObjectRenderer = GetComponent<Renderer>();
	}

	void OnMouseDown()
	{
		_isMat1 = !_isMat1;
		Material matToAffect = _isMat1 ? mat1 : mat2;
		_yourObjectRenderer.sharedMaterial = matToAffect;
	}
}

QUESTION : I am not sure if you are talking about a global clic or a clic on your object. The code above is for a click on the object, is it ok ?