Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by ILikeBigHutts · Mar 17, 2016 at 05:29 PM · unity 5ui

IsPointerOverGameObject() always true

Hey guys, I've been trying to figure out how block clicks in my game scene with UI objects, but I'm running into problems.

I've set up this little debug code here

if (Input.GetMouseButtonDown (0)) {

         if(EventSystem.current.IsPointerOverGameObject()){
             Debug.Log ("Clicked on UI");
         }
         else{
             Debug.Log ("Clicked on Space");
         }

     }

This is running in the update function, and whenever I click in the game scene, even when it's not over the UI, unity returns "Clicked on UI". I have a UI Canvas that covers the entire screen, and an inactive UI Panel that covers the game screen, but I have a CanvasGroup on that panel with Interactable and Blocks Raycasts set to false when it's deactivated. If anyone can help me understand why IsPointerOverGameObject is always returning true I'd greatly appreciate it.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Fredex8 · Mar 17, 2016 at 05:57 PM

I think this should solve your problem but I'll explain what I needed it for first.

My issue was that I was using OnMouseDown() on elements in my scene to hide my interface but since some buttons in my interface were in front of those objects the raycast would ignore the button completely and click the scene object, hiding the UI.

The solution was to put this on my scene objects (not the buttons).

     void OnMouseDown()
     {
         bool blockedByInterface = EventSystem.current.IsPointerOverGameObject();
 
         if (blockedByInterface == false)
         {
              //Hide UI
         }
     }

Another solution I believe is just to check the blocks raycast option on your interface elements but it seemed easier to me to just stick this script on my scene objects instead.

Comment
Add comment · 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
-1

Answer by nicmarxp · Nov 03, 2018 at 10:19 PM

If you're like me and going crazy that IsPointerOverGameObject() always returns true, try this solution instead:

 if (EventSystem.current.currentSelectedGameObject == null) {
     // This code will not run if you clicked the UI
 }

This worked great for me, so I ignore any clicks which is on the UI.

Another reason it might always return true, is if you check it when you're not clicking, but the solution above has worked best for me.

Good luck! :)

Comment
Add comment · 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
0

Answer by Prog_Meisters · Feb 22, 2021 at 11:08 PM

Check the Event Mask in the Physics Raycaster on your camera. IsPointerOverGameObject() will return true if the mouse is over any objects on those layers.

If you only want to check for the UI layer, make sure that only UI is checked.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Detect if UI Image is in a trigger 1 Answer

How to shrink text size 1 Answer

Check if visible in UI Mask 2 Answers

Button only works once 1 Answer

How do I save and load the value of a text? 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