Overlapping GUI Button priority

I’m making a game which has complex GUI windows and pop-ups, and a 3D world.

I have made a system to prevent the physics raycasts from occurring during mouse-clicks on the GUI, but that’s not what my problem is.

My problem is that when a pop-up with an “ok” button appears, and that button happens to line up with a button on the window behind, it clicks the button on the behind window and not the pop-up window button in front.

Is there a way to tell Unity which of two overlapping buttons I want it to listen to first?

If they’re drawn from separate scripts then try using GUI.depth

I made my popup windows their own script. I made it so that each popup was the same exact instance that appeared and disappeared, so I could make it static. Then I changed every time I have a button in my program to check if the popup is there. If the popup is visible, nobody draws any buttons, so there can be no accidental clicking. Hacky but functional.

you can put button in different layer and when you use raycast defina a ;ayer to which you want recieve touches

Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;

in this change “DefaultRaycastLayers” time by time it will recieve touches in only given layers.