block clicking gameobject behind ui

im using

public void OnMouseDown(){
         print("this gameobject is clicked")

}

but i would like to block mouse clicking it when im clicking the ui(space overlay)
i think blocksraycasr thing doesnt work…
i want to using worldspace ui for the solution or add variables to check if the ui in on…
any fundamental solutions for this???

I think what you are looking for is LayerMask. Here is a very detailed explanation:

here is a simpler answer that is more tuned towards your question: