- Home /
Question by
DiamondCharge · Sep 15, 2020 at 11:28 AM ·
c#button2d game
Checking if Button is not pressed
I am making a Shield for my 2D game and I enable it as soon as I press "Fire2", however it does not get disabled after I let go of the button. I have tried to use an else command to disable it, but I cannot get Unity to detect me leaving the button. Here is the code, please help!
void Shield()
{
if(Input.GetButtonDown("Fire2"))
{
Shield1.SetActive(true);
}
//This Does Not Work
if(Input.GetButtonUp("Fire2"))
{
Shield1.SetActive(false);
}
}
Comment
Answer by Jenmar75 · Sep 15, 2020 at 11:24 PM
I would use "!" instead of using "GetButtonUp" So instead of reading:
void Shield()
{
if(Input.GetButtonDown("Fire2"))
{
Shield1.SetActive(true);
}
//This Does Not Work
if(Input.GetButtonUp("Fire2"))
{
Shield1.SetActive(false);
}
}
It could read:
void Shield()
{
if(Input.GetButtonDown("Fire2"))
{
Shield1.SetActive(true);
}
else if(!Input.GetButtonDown("Fire2"))
{
Shield1.SetActive(false);
}
}