Differentiate on click anywhere vs click on button...

Hi guys,

In my game I want an object being turned off when a click is being pressed down, and then re-activated when mouse click is released… like the below

		if (Input.GetMouseButtonDown (0)) {
			centerPiece.SetActive (false);
		}
		if (Input.GetMouseButtonUp (0)) {
			centerPiece.SetActive (true);
		}

This part is okay but the problem is when I click my pause button it obviously turns on, and off the game object when I click the button, which I don’t wan’t to happen…

Any wiser alternatives to approaching this goal, or is there a function to differentiate a click on everywhere else, but the button?

The button was created on a Canvas and is a child of a canvas, not script built.

I’m still a noob so be patient and nice please :wink:

This could help you out - it’s a function I found somewhere that tells you whether or not the pointer is over a UI object:

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

Now you can do something like:

if (Input.GetMouseButtonDown (0) && !IsPointerOverUIObject()) 
{
    centerPiece.SetActive (false);
}
if (Input.GetMouseButtonUp (0) && !IsPointerOverUIObject())
{
    centerPiece.SetActive (true);
}