Changing material of an object

Hi. I looked for a solution to this but couldn’t find anything. What i want to do is change the material of an object every few seconds. Here is my code:

	private float countdown;
	public float originalCountdown;
	public Material white, black;
	
	void Start () {
		countdown = originalCountdown;
	}
	
	void Update() {
		countdown -= Time.deltaTime;
		if (countdown <= 0) {
			Change ();
		}		
	}

	void Change() {
		countdown = originalCountdown;
		if (renderer.material = black) {
			Debug.Log ("White");
			renderer.material = white;
		} else if (renderer.material = white) {
			Debug.Log ("Black");
			renderer.material = black;
		}
	}
}

What is happening now is that the material changes from black to white and i receive the message “White”. Then the material stays white and i keep receiving the message “White” every time that Change() is called.

I really can’t understand what is bad with the code. Also i am not using == because it only works with =. Not sure why.

Pretty new to all this anyway.

For comparison operations, you need to use double equals (‘==’). In addition, since you are switching, you don’t need the else:

 void Change() {
       countdown = originalCountdown;
       if (renderer.material == black) {
         Debug.Log ("White");
         renderer.material = white;
       } 
    else {
         Debug.Log ("Black");
         renderer.material = black;
       }

}