Detect only UI button click

In HUD panel of game, I have set Pause game button. Behind this, there is big collider object in game play which is also clickable.

So at present, when I click on Pause button, both event get executed. Pause button click and Collider Raycast code. I want to implement like, at area of Pause button only that button action get executed not Collider Raycast.

Please give me some help into this.

Totally credit goes to @fabian.mkv and for this post

This working like a charm :slight_smile:

IsPointerOverGameObject not working with touch input

 private bool IsPointerOverUIObject() {
     PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
     eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     List<RaycastResult> results = new List<RaycastResult>();
     EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
     return results.Count > 0;
 }

I think somwhere in the canvas or the button itself should be a checkbox saying “Block raycast” or something similar

if(!EventSystem.current.IsPointerOverGameObject())
{
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if (Physics.Raycast(cameraRay , out raycastHit))
{
//Do something with hit collider.
}
}

EventSystem.current.IsPointerOverGameObject() is the way to separate clicks on UI game object and clicks on ‘in scene’ game objects.