Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detecting on which UI element was clicked.

Discussion in 'UGUI & TextMesh Pro' started by laur-ganta, May 12, 2015.

  1. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    Code (CSharp):
    1. if(EventSystem.current.IsPointerOverGameObject())
    2.             {
    3.                 if( !EventSystem.current.currentSelectedGameObject.CompareTag("BuffIndicator") )
    4.                     return;
    5.             }
    Hello! As the code above shows I want to block the events only for objects that are not tagged "BuffIndicator".(I use this at start of an input function)

    The problem is that it detects I have clicked on a UI element, but the buffindicator is an image and the currentSelectedGameObject is null, unless i click a button.

    How do I properly implement this?


    EDIT:

    I added a Button component to my indicator, I set it's transition to none and left the listener list empty. But the currentSelectedGameObject is gets set to it only on MouseButtonUp, and I need this code in GetOnMouseButton(), which means if the cursor enters the indicator area while is clicked, it will be still null.

    Any workaround for this? Please!
     
    Last edited: May 12, 2015
  2. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
  3. laur-ganta

    laur-ganta

    Joined:
    Oct 22, 2014
    Posts:
    24
    I found a way for this:

    I added an EventTrigger on each of the elements I wanted to disable the control in that script, that has listeners for PointerEnter and PointerExit. On enter disables the control in that script, on exit it enables it. I'll use this method from now on on similar situation.
     
    GebhardtXR likes this.