Prevent clicking collider behind Unity UI Toolkit

Hey,

I have one button that is infront of my 2D Tilemap, yet clicks are being registered for the tilemap and not the button.

This only happens if the tilemap has a 2D Tilemap Collider but I need this collider so I can deselect my game objects. If the 2D Tilemap Collider is removed, I can once again, click my button.

I’ve tried using the sorting layers, layers and setting the positional z to - and + but the 2D Tilemap Collider still takes precedence over the UI Toolkit Button I’ve made.

Any ideas?

Does sound like the EventSystem is getting confused. Could you post the code used for selecting the tilemap?

Hey Huwp,

Sure, I went with two approaches but both don’t work - here’s one with OnMouseDown (this one lets me click behind my 3 buttons)

        public void OnMouseDown()
    {
      
        foreach(Building obj in Building.moveableObjects)
        {
            if(obj != this)
            {
                obj.selected = false;
                obj.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
            }
        }  
    }

here’s one using OnMouseUp but it utilises the IsPointerOverGameObject (this one constantly says I’m clicking on the UI, when I’m not, I have 3 buttons)

     private void OnMouseUp()
     {
         ClickUI = false;
         if (EventSystem.current.IsPointerOverGameObject())
         {
             if (EventSystem.current.currentSelectedGameObject.name == "IcoDropDown")
             {
                 ClickUI = true;
             }
         }
       
         if(ClickUI){
            Debug.Log("Click UI button");
         }
     }

Here is how my UI Toolkit is set up too, you can see the Container doesn’t go over the entire gameview. The VisualElement (parent) does but it has Picking Mode set to Ignore

I don’t have the answer exactly, but I can see a problem. The code is structured in a way that’s going to make debugging difficult.

First thing that needs doing is separating the tasks:

void SelectTower(GameObject target) {
  Debug.Log("select " + target.name);
  foreach(Building obj in Building.moveableObjects)
  {
    if(obj != target) { /* etc */ }
  }
}

Next the differences in the approach should be eliminated.

void OnMouseDown() { Debug.Log("mousedown"); Approach(); }
void OnMouseUp() { Debug.Log("mouseup"); Approach(); }
void Approach()
{
  if(EventSystem.current.IsPointerOverGameObject())
  {
    var o = EventSystem.current.currentSelectedGameObject;
    Debug.Log("is over gameobject " + o.name);
    if(o.name == "IcoDropDown")
    {
      SelectTower(o);
    }
  }
}

Please share if you discover anything with those changes.

1 Like

That helped a lot in determining what is going on Huwp, thanks for the idea.

Unfortunately it’s as I thought, when I click on the UI Toolkit Button, it triggers both the button and thinks I’m clicking on the background tilemap at the same time.

         if (EventSystem.current.IsPointerOverGameObject())
         {
                 var o = EventSystem.current.currentSelectedGameObject;
               
                 if(o != null)
                 {
                 Debug.Log("is over gameobject " + o.name);
               
                     if(o.name == "PanelSettings")
                    {
                    
                    }
                 }
                 else
                 {
                    Debug.Log("is clicking bg" );
                   
                    foreach(Building obj in Building.moveableObjects)
                    {
                        if(obj != this)
                        {
                            obj.selected = false;
                            obj.HidePoints();
                            obj.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
                        }
                    }  
                 }
         }

“is clicking bg” & “is over gameobject " + o.name)” is being triggered at the same time if a button is pressed.

PanelSettings is the name of the buttons from the toolit.

Not sure why mouse events are getting picked up if a button is there!

After a more thorough check, it seems to work now! I removed OnMouseDown as that seems to be getting the background all the time, whereas OnMouseUp always got the UI Button - can’t re-create my issue anymore!

Thanks a lot for the pointers & idea, Huwp!

1 Like