- Home /
How to set a boolean to true, when pressing a button, and keep it without changing to false again,How to set a boolean true when pressing a button, without letting it change to false again?
Hi, I wanted to check if my player is holding a gun, so I can play different animations. I set a bool as a parameter called HoldingGun (false). I wanted to make it true when the player hit the key ("g"). It is like a mode change from Idle to shooter. and that works, except that the boolean is just true, as long as I am pressing "g", what is not what I want. Is there a way to set a boolean true, as long as I press another key? And without holding the "G" key?
Here is my code:
void Update()
{
animator.SetBool("HoldingGun", false);
if (Input.GetKey("g"))
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}
Answer by Hellium · Jun 14, 2019 at 08:35 PM
void Start()
{
animator.SetBool("HoldingGun", false);
}
void Update()
{
if (Input.GetKeyDown("g") && !HoldGun)
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}
}
Answer by L33T_Rakshasa · Jun 14, 2019 at 08:47 PM
@unity_3DmGkY57st7DXQ I don't know if it will help but if you are wanting like a toggle type of effect when ever you press the key you set up you could just set the bool value to be what it isn't.
Example: if (Input.GetKeyDown(KeyCode.J)) { Hand = !Hand; }
This would make it so if the value of Hand is false, pressing the button would change it to true. Pressing it again would change it back to false.
I don't know if this helps but just a suggestion for having the value invert on button press when dealing with bools.
Wow, thank you very much! I was thinking about a longer way to do that. That worked! and it helps. thanks again. ( for anyone who had the same question, my end code looks like this now:)
bool HoldingGun = false;
void Start()
{
animator.SetBool("HoldingGun", HoldingGun);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown("g"))
{
HoldingGun = !HoldingGun;
animator.SetBool("HoldingGun", HoldingGun);
}
Answer by SavaTim · Jun 14, 2019 at 08:12 PM
void Update() { animator.SetBool("HoldingGun", false);
if (Input.GetKeyDown("g"))
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}
when doing this, the boolean gets false again in just a few seconds... I want to set it as true, as long as I don't change it again. thanks for the answer though.
set a boolean true when you Press "G" and set fals when you return to prass "G" is it? OR set a boolean true when you Press "G" $$anonymous$$eep true when you press "$$anonymous$$ouse 1" and set fals when You GetUp "$$anonymous$$ouse 1"????
yes, but the boolean returns to false, even though I didn't press "G" again. It just gets true for a few seconds after pressing "G"
Its Working Fine For me
public bool Hand;
private void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.J))
{
if (Hand == false)
{
StartCoroutine(TimeTrue());
}
else
{
StartCoroutine(TimeFalse());
}
}
}
IEnumerator TimeTrue()
{
yield return new WaitForSeconds(5);
Hand = true;
}
IEnumerator TimeFalse()
{
yield return new WaitForSeconds(5);
Hand = false;
}
Your answer
Follow this Question
Related Questions
If statement breaking button color copy 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
(C#) If statements based on variables in another script 1 Answer