Disabling Click Through?

I have a point in my code where I get the GetMouseButtonDown from the Input and when I put a button on the screen it still activates the GetMouseButtonDown since its not very distinct on when it should ignore if a GUI element has been clicked. Is there a good place I can look up how to find what element was clicked so that I can ignore any “passthrough” that might happen to the screen beyond the GUI?

I find that as a general rule you should attach a script that uses OnMouseUp to the specific GameObject that you want to have the behavior with. That way if there is something else in the way, it will register the click on that other object and not on something else that is obscured (or even not in view of the camera). I suggest OnMouseUp because that guarantees that the player has performed a full click on the object.

The OnMouseDown, OnMouseUp, OnMouseEnter, OnMouseExit, OnMouseOver and OnMouseDrag methods of MonoBehaviour are really great tools for mouse interaction that you can use with any object in Unity.

Oh, so you could in theory put a OnMouseUp in a script on the Terrain so that way you can register anytime they click specifically the terrain? This way you could avoid having to worry about characters, units, objects, and the like being clicked on as well?

So long as the terrain has a Collider attached to it, then yes, it will only register a click when that Collider is clicked on and not any other Colliders or GUIElements that may be in front of it.

Just to note, the Unity terrain always has a collider attached and doesn’t need one added. (It’s a heightmap collider; actually it would be nice to be able to use those outside of the terrain system…)

–Eric