- Home /
Listener and multiple quick presses on button
Hello!
If my button has this logic on click, can the HomeMenuButton_OnClick method be called several times when the homeMenuButton button is clicked more one time (fast clicks)? Thanks!
homeMenuButton.onClick.AddListener (HomeMenuButton_OnClick);
Answer by ranch000 · Jun 10, 2020 at 07:01 AM
It is a good practice to remove the particular listener before adding it (in cases where your code is inside an OnEnable). This will prevent the listener triggering multiple times per single click
homeMenuButton.onClick.RemoveListener (HomeMenuButton_OnClick);
homeMenuButton.onClick.AddListener (HomeMenuButton_OnClick);
Also, for the fast clicks part set a boolean inside the method and perform method actions only if boolean is unset. Also remember to unset the boolean wherever required (Like re enabling your button UI)
bool clicked = false;
void HomeMenuButton_onClick
{
if(!clicked)
{
//actions
clicked = true;
}
}
Thanks brother, Listener was stacking on my object. so I added Button.onClick.RemoveAllListeners() and everything is now working
Your answer

Follow this Question
Related Questions
Two audio listeners - when loading a level 2 Answers
Pan all input sound? 0 Answers
My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer
Script Not Attached to Scene Still Triggers Events 0 Answers
What is the current workaround for AudioSource.ignoreListenerPause in Unity 4.6? 1 Answer