- Home /
Command "GetButton" not working well
I was making a Firethrower script on JavaScript, and I read that the command "GetButton" sets "true" when the key is holded, and sets "false" when not. But in my case, when I press the key, becomes "true", and doesn't come false never. There's the code:
PS: Sorry for my English.
Answer by Dave-Carlile · Jul 28, 2015 at 08:52 PM
You need to handle the false condition to set the flameThrower back to inactive...
if (Input.GetButton("Fire1"))
{
flameThrower.SetActive(true);
}
else
{
flameThrower.SetActive(false);
}
The code you have now is activating the flamethrower when the button is first pressed, but you don't have anything to deactivate it. When GetButton returns false in my sample code, flameThrower will be set to inactive.
You can shorten this up a bit by just passing the boolean result of GetButton directly to the SetActive method.
flameThrower.SetActive(GetButton("Fire1"));
But whether or not you want to do that depends on if you have other things to do with the button is held down or not.
Thanks! It worked very well! (Sorry if I am too much noob, I'm using Unity for three days).
Everyone has to be a noob at some point :). No need for apologies.
Answer by Djohnnys · Jul 28, 2015 at 09:53 PM
If I understand correctly you want the flamethrower to only be active when the button is pressed.
In this case you have forgotten to turn it of (set it to inactive) with an else statement.
function Update()
{
if (Input.GetButton("Fire1")
{
flameThrower.SetActive(true);
}
else //if the button is not pressed
{
flameThrower.SetActive(false);
}
}
I haven't tested your answer, but it seems that it would work. Thank you anywhere!
Your answer

Follow this Question
Related Questions
Ball jumping mechanic, scrip errors. 1 Answer
[UNSOLVED] Object reference not set to an instance of an object 0 Answers
Unity freezes when I add a special script 1 Answer
It is not possible to invoke an expression of type 'UnityEngine.GameObject'? 1 Answer
Sound system not working correctly 0 Answers