Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
18
Question by fabian-mkv · Dec 21, 2015 at 04:23 AM · touchonmousedown

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
89

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:

http://forum.unity3d.com/threads/ispointerovereventsystemobject-always-returns-false-on-mobile.265372/

I include that code and then just substitute EventSystem.current.IsPointerOverGameObject() with IsPointerOverUIObject() and it works.

Hope this helps someone.

Comment
Add comment · Show 21 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ramioooz · Jun 26, 2016 at 12:57 PM 0
Share

Tested. Working Perfectly. Thank you so much. Wishing you all success in your project.

avatar image amzin7000 · Aug 01, 2016 at 01:29 AM 0
Share

Dude thanks so much! I really still don't know what the deal is with IsPointerOverGameObject(pointerID) not working at all

avatar image hexagonius · Dec 18, 2016 at 09:11 PM 2
Share

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.

avatar image Ninjars hexagonius · Jun 23, 2018 at 05:01 PM 1
Share

Thank you for mentioning this! Both solutions work, but this one is all that I needed.

avatar image DapperDirewolf hexagonius · Oct 02, 2018 at 08:10 PM 0
Share

This solved my problem too.

avatar image mehradmbs · Nov 08, 2017 at 03:04 PM 1
Share

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;

avatar image lasdoo5 · May 19, 2018 at 07:01 PM 0
Share

thanks broo... :) its work fine

Show more comments
avatar image
18

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;
         }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndBje · May 04, 2018 at 06:21 PM 1
Share

Simple and effective. Thanks!

avatar image guetta18 · Aug 06, 2018 at 07:13 PM 0
Share

tnxxxxxxxxxx!!!! i just spent 2.5 hours to find to this problem solution!!!

avatar image NathanJSmith · Jan 11, 2020 at 03:34 AM 0
Share

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

avatar image
6

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;
         }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shadowing · Mar 08, 2020 at 01:40 PM 0
Share

This one doesn't work. Touch input gets by still. Tested on android Unity 2018.4.18

avatar image Wolar Shadowing · Mar 08, 2020 at 02:47 PM 0
Share

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.

avatar image Shadowing Wolar · Mar 08, 2020 at 02:55 PM 0
Share

I tried using it to ignore dragging of a worlds space map. With detecting touch on overlay canvas.

Show more comments
avatar image insoluzioni · Apr 08, 2020 at 03:03 AM 0
Share

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.

avatar image AdnanSiddique · Jul 15, 2020 at 08:39 AM 0
Share

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

avatar image
-1

Answer by ZenriS · Sep 03, 2018 at 06:34 PM

This workes great, thanks for the help

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SSStreetFighter · Jan 07, 2021 at 05:49 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How "select" the enemy even with joystick being used 0 Answers

Instantiate onMouseDown 1 Answer

Problem with touch detection 1 Answer

Simulated Touchscreen of the new input system problems 0 Answers

How do I use Input.GetButton, instead of OnMouse() 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges