UI buttons and touch input problem

Guys, I have one problem. I’m developing a mobile game which is a simple infinite runner. For player controls I using something like:

if (Input.touchCount > 0)
{
	for (int i = 0; i < Input.touchCount; i ++)
	{
		Touch touch = Input.GetTouch(i);
		if (touch.phase == TouchPhase.Began)
		{
			//Jump
		}
	}
}

The pause button is a UI element and when I’m pressing it player jumps but he is not supposed to.
Then I’ve made a check with EventSystemManager.currentSystem.IsPointerOverEventSystemObject() in player code. And while testing with Unity Remote it works fine, but after building project on Android device this doesn’t work, player still jumps. How to solve this problem? Maybe there is another way of checking?

I ran into this exact issue today. Apparently EventSystemManager.currentSystem.IsPointerOverEventSystemObject() always returns false in an actual APK build. The issue tracker was marked “By Design”, but it definitely seems like a bug to me.

Use this code as a work around (Credit goes to MWK888, found here: http://forum.unity3d.com/threads/ispointerovereventsystemobject-always-returns-false-on-mobile.265372/ ):

/// <summary>
/// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
/// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
/// </summary>
private bool IsPointerOverUIObject() {
    // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
    // the ray cast appears to require only eventData.position.
    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;
}
 
/// <summary>
/// Cast a ray to test if screenPosition is over any UI object in canvas. This is a replacement
/// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
/// </summary>
private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition) {
    // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
    // the ray cast appears to require only eventData.position.
    PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    eventDataCurrentPosition.position = screenPosition;
 
    GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
    List<RaycastResult> results = new List<RaycastResult>();
    uiRaycaster.Raycast(eventDataCurrentPosition, results);
    return results.Count > 0;
}

For touch input this code work for me

/// <summary>
    /// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
    /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
    /// </summary>
    private bool IsPointerOverUIObject(Touch t)
    {
        // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
        // the ray cast appears to require only eventData.position.
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(t.position.x, t.position.y);

        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        return results.Count > 0;
    }

the argument t is an element in Input.touches[]

Let try it from Unity Document: Redirect to... title of new-page

void Update () 
	{
		// Check if there is a touch
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
		{
			// Check if finger is over a UI element
			if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
			{
				Debug.Log("Touched the UI");
			}
		}	
	}