- Home /
Button On Click event firing GetMouseButton within script
So I have a button that runs a similar method as below when clicked, and the if statement is entered immediately but I'm wanting this if statement to be entered when the user clicks again after initially pressing the button, how can this be done?
public void ButtonCommand()
{
while(true)
{
if (Input.GetMouseButtonDown(0))
{
//do something
}
}
}
Answer by tmalhassan · Oct 11, 2017 at 02:58 PM
Hello @eddlilley. You can simply achive what you want by setting a bool. Try something like this:
private bool enableSecondClick = false;
public void Update()
{
if (enableSecondClick == true)
{
if (Input.GetMouseButtonDown(0))
{
// do something
// if you want this scenario to happen more than once, you can set the bool back to false
enableSecondClick = false;
}
}
}
public void ButtonCommand()
{
enableSecondClick = true;
}
Let me know if it worked.
All the best :)
@tmalhassan sorry I should have made it clear, the users second click can be anywhere on screen, not hitting the same button again.
I just updated the answer.
You could've simply moved the content to the update function. Following the updated answer, on the button click you will set the bool to true, which will allow the if
statement to take place.
I suggest updating your question to not confuse people :)
Your answer
Follow this Question
Related Questions
How do I make a function not appear on UnityEvents / Button OnClick lists? 0 Answers
Trouble detecting button release with Input.GetButtonUp() 1 Answer
Button Action Triggering Multiple Times Per Click? 1 Answer
why won't my add button script work?!? (help appreciated) 2 Answers
Multiple Cars not working 1 Answer