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:
    88
    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?
     
  2. teremy

    teremy

    Joined:
    May 2, 2015
    Posts:
    88
    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,197
    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 !!
     
    Joelspeanuts, PNUMIA-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:
    119
    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,859
    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!
     
  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. S3cubestudios

    S3cubestudios

    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:
    115
    Here to thank @teremy you saved med days of frustration !
     
    teremy likes this.
  14. LukasSacher

    LukasSacher

    Joined:
    May 2, 2019
    Posts:
    14
    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
    teremy and noio like this.
  15. LukasSacher

    LukasSacher

    Joined:
    May 2, 2019
    Posts:
    14
    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
    teremy, bruceweir1 and Allundo like this.
  16. Jamado_

    Jamado_

    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!
     
    teremy likes this.
  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 ...
     
    teremy and Niroan like this.
  18. lordubbe

    lordubbe

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

    iGoA

    Joined:
    Aug 23, 2020
    Posts:
    22
    Thanks! :) Saved me a lot of time!
     
    teremy likes this.
  20. emicenizo

    emicenizo

    Joined:
    Mar 26, 2016
    Posts:
    8
    Thanks mate!
     
    teremy likes this.
  21. octav88

    octav88

    Joined:
    Oct 19, 2019
    Posts:
    4
    I've tried every solution in the comments, however I'm still stuck on the problem. I'm using the new input system and all my buttons trigger the actions twice when clicking on them.

    Does anyone know other workarounds?

    EDIT: Found the solution 2 minutes after writing this.

    In the input debugger, I was simulating touch input with the mouse. This means that the button was once clicked and once touched.
    You can go into Window > Analysis > Input Debugger and adjust the settings there.
     
    Last edited: Jun 26, 2022
    teremy and TebogoWesi like this.
  22. ejderaltiparmak

    ejderaltiparmak

    Joined:
    May 1, 2020
    Posts:
    1
    You should apply your script to only button not for button text. Check it and if it is for both of them simply remove
     
    teremy likes this.
  23. OlafsOsh

    OlafsOsh

    Joined:
    Jul 10, 2018
    Posts:
    61
    November 3rd, 2022 salutes you, Sir!
     
    teremy and JoeStrout like this.
  24. jackjansen

    jackjansen

    Joined:
    Apr 4, 2019
    Posts:
    22
    I have the problem with the new input system, when using XR controllers. For some of my buttons I get two callbacks of my listener when I "press" the button with the ray. Not always, and not all buttons.

    None of the solutions here seem to be applicable, so I ended up writing code in the listener to simply ignore a second call if it's less than a second after the first call.
     
    teremy likes this.
  25. jackjansen

    jackjansen

    Joined:
    Apr 4, 2019
    Posts:
    22
    And then, an hour later, I find what my problem was. Posting here in case it helps someone. It turned out I had two XR ray interactors (plus line renderers, etc) on either hand. And this is surprisingly difficult to see, because the rays always overlap, and for most interactions it isn't a problem if it happens twice.
     
    mortezadianatfar and teremy like this.
  26. TebogoWesi

    TebogoWesi

    Joined:
    Aug 12, 2014
    Posts:
    159
    Looks like this is caused by not removing the listener OnDisable(). add button.OnClick.RemoveListner(Your Method)
     
    teremy likes this.
  27. Paulo33

    Paulo33

    Joined:
    Jun 25, 2013
    Posts:
    1
    2023,

    In my scenario, that problem are caused by adding a button inside a ListView, wich have their own "item selected" events and i also added the "clicked" event on button, this caused a two behaviour of "clicked and selected" events.
     
    teremy likes this.
  28. pumpkinszwan

    pumpkinszwan

    Joined:
    Feb 6, 2014
    Posts:
    214
    Just adding another possible solution for people: Steam. Steam hijacks gamepad input, and in my case it was causing the double button presses. Closing Steam resolved it for me.
     
    xucian and teremy like this.
  29. mortezadianatfar

    mortezadianatfar

    Joined:
    May 8, 2017
    Posts:
    6
    Thanks a lot, I face this issue when I am clicking UI button and create two instances from instantiate command.
    I am using Unity 2021.3.15f1, I didn't check it before or give any attention but when I used XR origin template it contained two ray interactor for each controller.

    But thanks a lot @jackjansen
     
    teremy and jackjansen like this.
  30. cwoppy

    cwoppy

    Joined:
    Sep 16, 2022
    Posts:
    2
    Writing from the future here in 2024 with a similar XR controller issue as above: pressing A on a joystick was firing off Select actions twice because of the Submit Alt Positive binds in the Input Manager. Removing them fixed it!