Button OnClick Event is firing twice

Hello.
I have the following code in an Awake-Function of a script:

sendButton = GameObject.FindGameObjectWithTag ("Chat_SendButton").GetComponent<Button>();
sendButton.onClick.AddListener(delegate{SendChatMessage();});

In SendChatMessage() there is just a simple Debug.log(“SendChatMessage”);.
Whenever I click on the button, the event is fired, if I then click somewhere else, the event is fired again ( im not even clicking on the button this time, though this happens only if there is no other input in between ).
The first event is fired when I no longer press the mouse Button on the Button, so its a mouseUp event that fires it, the second time when I click somewhere else and the event is fired, It fires even though my finger is still pressing down the left mouse button.

The documentation says:
http://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
“Note: Fires on MouseUp after MouseDown on the same object.”
But that’s not helping me. Is it perhabs a bug?

Why is it firing twice and is there a way to somehow get additional information about the event? Another question: How can I make it already fire when pressing down the mouse button?

5 Likes

I found the problem.
On my Input Settings I had “mouse 0” for Submit.
So whenever I click on the button it fires the event and also selects the Button. If I then click somewhere else then a Submit is triggered, so the event gets called again. Simply removing “mouse 0” as positive Button for Submit for now and everything works fine ;).

24 Likes

Oh … my… God… haha, I’m facing the exactly same issue and when I saw your solution I couldn’t believe, in my case I was setting the submit button and cancel button from the Event System to Fire1 and Fire2! Thanks a bunch, I would never figure this out by myself!

OMG i sat 8 HOURS on this problem and couldnt figure it out. 8 HOURS. Do you know the feeling you get when you sit 8 hours on an error and then finally find the solution…

I want to marry you my friend !!

3 Likes

You saved my life with that “I found the problem.” 3 DAYs thinking what’s wrong with the code when the error was in the input manager. WTF thanks a lot

1 Like

Many cheers, thanks a lot.

1 Like

Saved me a ton of time. Thank you!

1 Like

dude that saved me a ton

1 Like

Likewise! @teremy , you’re probably long gone, but we from the future thank you in the past for kindly documenting this obscure problem. You have saved me (and many others, apparently) a lot of time!

4 Likes

Here to give the somewhat yearly “thanks you saved me a ton” comment for 2020.

@teremy Thanks you saved me a ton.

2 Likes

I had the same issue. What I did is, I changed the button. It worked for me. It might be some unity bug. The same event worked for different button firing only once.

Give This solution a try. Maybe it will work for you too.

1 Like

Here to give the somewhat yearly “thanks you saved me a ton” comment for autumn 2020!

@teremy Thanks you saved me a ton.

1 Like

Here to thank @teremy you saved med days of frustration !

1 Like

Aight, have the same problem. Not my fault tho. I’m using the Unity input system and have the simulated touchscreen enabled. Now my button events get fired for the simulated touch and for my mouse click. Disableing the mouse device in the editor doesn’t work either since it can’t simulate the touches then.

Does anyone have a solution which isn’t adding a new section two my Input Asset which simulates the touches or checking with a bool that every button can be only pressed once in a short amount of time?

Edit: It calls the OnClick event twice instantly when I click the button once.

2 Likes

Fixed it quite quickly. I double clicked the “DefaultInputAction” asset which is the “Action Asset” on the EventSystems “InputSystemUIInputModule”. This brings up the Input Asset for “Player” and “UI”. In UI, search for the “Point” Action and delete the “Position [Mouse]” binding.

In case you want to have it back at some point, “/position” is the path.

Hope it helps :smile:

UPDATE: Problem with that is, you can’t have buttons in the absolute lower left corner. I think it’s because it crosses the 0, 0 screen position then and there is a pointer lying around there.
Didn’t test it with a touch screen. But you might be allowed to put a button there if you are using one.

3 Likes

THANKS YOU! My problem/fix was that i was using the “return” key to press a button and my submit key was return!

1 Like

Thank you so much! It worked perfectly.
I’ve struggled whole day with this double event firing problem.
New InputSystem brings us complexity …

2 Likes

Just leaving this here in case someone else did the same stupid mistake as me -
I had set up the button to use animations, and each animation triggered an Animation Event in the first frame. The reason I experienced onClick executing multiple times is because I had created a ‘void Press()’ method that the Animation Event invoked, but I did not consider that the UI.Button class uses a ‘Press’ method internally to trigger the event, meaning I got the onClick from actually pressing, but then also from the Animation Event

3 Likes

Thanks! :slight_smile: Saved me a lot of time!

1 Like

Thanks mate!

1 Like