Search Unity

Button OnClick Event is firing twice

Discussion in 'UGUI & TextMesh Pro' started by teremy, Oct 8, 2015.

  1. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    79
    Hello.
    I have the following code in an Awake-Function of a script:
    Code (CSharp):
    1. sendButton = GameObject.FindGameObjectWithTag ("Chat_SendButton").GetComponent<Button>();
    2. 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?
     
    anycolourulike and Dudey like this.
  2. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    79
    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 ;).
     
  3. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,168
    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!
     
  4. pascal281

    pascal281

    Joined:
    Aug 3, 2017
    Posts:
    1
    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 !!
     
    LUCID-Rob and teremy like this.
  5. Sfogatto

    Sfogatto

    Joined:
    Oct 8, 2015
    Posts:
    1
    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
     
    teremy likes this.
  6. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    111
    Many cheers, thanks a lot.
     
    teremy likes this.
  7. omeys

    omeys

    Joined:
    Oct 29, 2015
    Posts:
    5
    Saved me a ton of time. Thank you!
     
    teremy likes this.
  8. midnightcrawler1

    midnightcrawler1

    Joined:
    Jan 17, 2017
    Posts:
    15
    dude that saved me a ton
     
    teremy likes this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,685
    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!
     
    trombonaut, J_Smu and teremy like this.
  10. Dudey

    Dudey

    Joined:
    May 28, 2019
    Posts:
    3
    Here to give the somewhat yearly "thanks you saved me a ton" comment for 2020.

    @teremy Thanks you saved me a ton.
     
    trombonaut and teremy like this.
  11. Sarath886

    Sarath886

    Joined:
    Jun 8, 2015
    Posts:
    4
    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.
     
    teremy likes this.
  12. mattSydney

    mattSydney

    Joined:
    Nov 10, 2011
    Posts:
    171
    Here to give the somewhat yearly "thanks you saved me a ton" comment for autumn 2020!

    @teremy Thanks you saved me a ton.
     
    teremy likes this.
  13. Niroan

    Niroan

    Joined:
    Jun 16, 2019
    Posts:
    70
    Here to thank @teremy you saved med days of frustration !
     
  14. NotCqmo

    NotCqmo

    Joined:
    May 2, 2019
    Posts:
    12
    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.
     
    Last edited: Dec 31, 2020
    noio likes this.
  15. NotCqmo

    NotCqmo

    Joined:
    May 2, 2019
    Posts:
    12
    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, "<Mouse>/position" is the path.

    Hope it helps :D

    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.
     
    Last edited: Dec 31, 2020
    bruceweir1 and Allundo like this.
  16. berrangejanko

    berrangejanko

    Joined:
    Oct 29, 2020
    Posts:
    2
    THANKS YOU! My problem/fix was that i was using the "return" key to press a button and my submit key was return!
     
  17. Allundo

    Allundo

    Joined:
    Sep 7, 2020
    Posts:
    1
    Thank you so much! It worked perfectly.
    I've struggled whole day with this double event firing problem.
    New InputSystem brings us complexity ...
     
    Niroan likes this.
  18. lordubbe

    lordubbe

    Joined:
    Feb 7, 2015
    Posts:
    2
    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
     
    Niroan and JoeStrout like this.
  19. iGoA

    iGoA

    Joined:
    Aug 23, 2020
    Posts:
    9
    Thanks! :) Saved me a lot of time!
     
unityunity