Answers for "unity key toggle"

C#
0

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);
	}
}
Posted by: Guest on October-23-2020
0

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();
}
Posted by: Guest on October-23-2020

C# Answers by Framework

Browse Popular Code Answers by Language