Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Mouse click detected twice on UI eventsystem

Discussion in 'Input System' started by kurai, Jan 21, 2023.

  1. kurai

    kurai

    Joined:
    Jan 9, 2012
    Posts:
    118
    Hi everyone,
    I am building a little dialogue system using the new input system. I have a system of buttons which are populated dynamically with the options (using Ink for this, but it's not the issue).
    I set up my actions by binding the mouse click and space key to the Click action. I have set up the UI event system and I have put on my storyManager object the Player input component set to read the UI section of my event map.
    Problem is, everytime I click the button, the onclick event fires twice, causing an exception in the engine.
    Here is the code I use to set a listener up in my button:

    Code (CSharp):
    1.  Choice choice = story.currentChoices[i];
    2.                 _buttons[i].interactable = true;
    3.                 _buttonLabels[i].text = choice.text.Trim();
    4.                 _buttons[i].onClick.AddListener(delegate
    5.                 {
    6.                     OnClickChoiceButton(choice);
    7.                 });
    If I debug.log the method OnClickChoicebutton I can see that everytime I click the code runs twice.

    Code (CSharp):
    1. void OnClickChoiceButton (Choice choice)
    2.     {
    3.         //_optionsPresented = false;
    4.         Debug.Log(choice.index);
    5.         Debug.Log(_optionsPresented);
    6.         setUIElement(_dialogueGroup,true);
    7.         setUIElement(_optionsGroup, false);
    8.         story.ChooseChoiceIndex (choice.index);
    9.         RefreshStory();
    10.     }
    Why it happens? Is it normal? I cannot find a way to have the mouse click work only once.
    Help?
     
  2. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Are you sure the event
    onClick
    is fired once, not twice? Is there a
    active 
    property of the captured event that gets value 0 or 1 for Click on and Click release?
    Are you sure you have one button, not two in the same position?
    Try to bind static
    OnClickChoiceButton
    and use passed
    sender
    parameter.
    Try to debug and check call stack.

    Or maybe you've already solved that issue?