How to toggle a box color on & Off?

Hello guys,

Im completely new to programming, and I’ve been trying for days to get a cubes color to turn from white to red at the press of the T button, and to turn from Red to White at the press of the T button again. Can someone tell me where i’m going wrong here is my code that I’ve tried. Im currently trying to learn C#.

public class colortogglesimple : MonoBehaviour {

	void Start (){
		}
	 
	void Update(){
	
		if (Input.GetKeyDown ("t")) 

			renderer.material.color = Color.red;

		else
			renderer.material.color = Color.white;

	}

}

Something like this should work.

Color lastColor = Color.white;

void Start(){
    renderer.material.color = lastColor;
}

void Update(){
    if(Input.GetKeyDown(KeyCode.T)){
        if(lastColor == Color.red) lastColor = Color.white;
        else lastColor = Color.red;

        renderer.material.color = lastColor;
    }
}