IsPointerOverGameObject not working with touch input
I'm using IsPointerOverGameObject() inside OnMouseDown() to detect whether the player is clicking/touching a GUI button that happens to be over the game object. If that's the case, the function ignores the remainder of the code in OnMouseDown() by immediately returning:
void OnMouseDown()
{
// Detect mouse event
if (EventSystem.current.IsPointerOverGameObject())
{
print("return mouse");
return;
}
// Detect touch event
foreach (var touch in Input.touches)
{
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
print("return touch");
return;
}
}
// Other code...
}
In the editor, this works as expected when clicking a GUI button over the game object: the function prints "return mouse" and returns. On my Android device, the GUI object is interacted with, but the OnMouseDown() function doesn't return!
How can I fix this problem?
Answer by fabian-mkv · Dec 21, 2015 at 05:07 AM
After some hours, I managed to find a work around:
private bool IsPointerOverUIObject() {
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;
}
Taken from this thread:
I include that code and then just substitute EventSystem.current.IsPointerOverGameObject() with IsPointerOverUIObject() and it works.
Hope this helps someone.
Tested. Working Perfectly. Thank you so much. Wishing you all success in your project.
Dude thanks so much! I really still don't know what the deal is with IsPointerOverGameObject(pointerID)
not working at all
what version of Unity are you running this on?
I remember fixing that problem for a single touch game by passing 0 as the parameter.
Thank you for mentioning this! Both solutions work, but this one is all that I needed.
Its perfect solution. i want to exclude one UI object. (pause button) i set tag for them and use:
if(results.Count > 0) return results[0].gameObject.tag == "excludeUiTouch"; else return false;
Answer by slake_it · Mar 07, 2018 at 06:05 AM
from unity docs
If you use IsPointerOverGameObject() without a parameter, it points to the "left mouse button" (pointerId = -1); therefore when you use IsPointerOverGameObject for touch, you should consider passing a pointerId to it.
https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html
you can use this code to check touch & mouse
/// <returns>true if mouse or first touch is over any event system object ( usually gui elements )</returns>
public static bool IsPointerOverGameObject(){
//check mouse
if(EventSystem.current.IsPointerOverGameObject())
return true;
//check touch
if(Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began ){
if(EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
return true;
}
return false;
}
tnxxxxxxxxxx!!!! i just spent 2.5 hours to find to this problem solution!!!
True, but should use Input.GetTouch(0) ins$$anonymous$$d of Input.touches[0] (since Input.touches allocate temporary variables). The example for can be found at: https://docs.unity3d.com/530/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html
Answer by Wolar · Jun 25, 2019 at 12:41 PM
For future visitors, as far as I know, both of the solutions above generates quite a lot of garbage. Also one of them only supports single touch. So as far as I can tell, I think the solution should be something like
public static bool IsPointerOverGameObject()
{
// Check mouse
if (EventSystem.current.IsPointerOverGameObject())
{
return true;
}
// Check touches
for (int i = 0; i < Input.touchCount; i++)
{
var touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
return true;
}
}
}
return false;
}
This one doesn't work. Touch input gets by still. Tested on android Unity 2018.4.18
We use exactly this code on our production apps and it works fine. We are using Overlay canvases, might work differently with Camera / World space canvases. Also as you can see in the code, it only checks for TouchPhase.Began (as we needed only to stop clicks) so if you need to check at different phases of the touch, you might want to modify the code. We used it since something like Unity 2018.3.x to 2019.2.x and it worked always fine so I think it should work in 2018 LTS version as well.
I tried using it to ignore dragging of a worlds space map. With detecting touch on overlay canvas.
You'll have to disable the first check (mouse) for this to work properly on a device. $$anonymous$$ouse clicks behaves like touches on a mobile device, so if you have the first check there, you'll end up with weird behaviours.
Above code is not working in my case, Canvas render mode is camera space but this code worked fine
private bool IsPointerOverUIObject() { PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); List results = new List(); EventSystem.current.RaycastAll(eventDataCurrentPosition, results); return results.Count > 0; }
Hope it will help other
Answer by ZenriS · Sep 03, 2018 at 06:34 PM
This workes great, thanks for the help
$$anonymous$$y code only allows my agent to move to UI elements and voids touches on the map. public class $$anonymous$$ovement : $$anonymous$$onoBehaviour { public GameObject RallyPoint;
void Update()
{
if (Input.touchCount > 0)
{
RaycastHit hit;
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = touch.position;
Ray ray = Camera.main.ScreenPointToRay(touchPosition);
if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity))
{
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
Vector3 newPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);
RallyPoint.transform.position = newPosition;
}
}
}
}
}
does anyone know what I am doing wrong?
Your answer

Follow this Question
Related Questions
How "select" the enemy even with joystick being used 0 Answers
Instantiate onMouseDown 1 Answer
Problem with touch detection 1 Answer
UIButton not working when testing on mobile device 0 Answers
buttons vs touch. 1 Answer