toogle key in unity code example

Example 1: unity key toggle

//This is a first simple example of a toggle using a keyboard key pressed down:
//Variables
public GameObject ON;
public GameObject OFF;
public KeyCode Interact = KeyCode.F;
private bool On_Off = false;


//you can use a void Update, a function or a IEnumerator.
private void Update()
{
    if (Input.GetKeyDown(Interact))
	{
         	while (true)
         	{
             	if (On_Off == false)
             	{
                	ON.SetActive(true);
                	OFF.SetActive(false);
                	On_Off = true;
                	break;
             	}
             	if (On_Off == true)
             	{
             		ON.SetActive(false);
                	OFF.SetActive(true);
                	On_Off = false;
                	break;
             	}
         	}
	}
	if (On_Off == false)
	{
		ON.SetActive(false);
    	OFF.SetActive(true);
	}
	if (On_Off == true)
	{
		ON.SetActive(true);
    	OFF.SetActive(false);
	}
}

Example 2: unity key toggle

//This is a second simple example of a toggle using a keyboard key pressed down:
//Variables
public GameObject ON;
public GameObject OFF;
public KeyCode Interact = KeyCode.F;
private bool On_Off = false;

//you can use a void Update, a function or a IEnumerator.
public IEnumerator Toggle()
{
    if (Input.GetKeyDown(Interact))
	{
         	while (true)
         	{
             	if (On_Off == false)
             	{
                	ON.SetActive(true);
                	OFF.SetActive(false);
                	On_Off = true;
                	break;
             	}
             	if (On_Off == true)
             	{
             		ON.SetActive(false);
                	OFF.SetActive(true);
                	On_Off = false;
                	break;
             	}
         	}
	}
	if (On_Off == false)
	{
		ON.SetActive(false);
    	OFF.SetActive(true);
	}
	if (On_Off == true)
	{
		ON.SetActive(true);
    	OFF.SetActive(false);
	}
}

private void Update
{
	Toggle();
}