• 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
6
Question by KingRecycle · Nov 02, 2014 at 09:58 AM · uiraycasttarget4.6spell

How to prevent raycast when clicking 4.6 UI?

I have it so I can click enemies to target them and if the player clicks anything else it clears the target but if I want to then click my spell icon to cast a spell it will detect terrain or whatever is under the UI object and reset my target.

How can I prevent this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
30

Answer by troien · Nov 02, 2014 at 11:31 AM

You could check for the following to be false before you do your own raycast.

 UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()

If this value is true, you should be hovering any of your UI elements, if false not.

On a sidenote, If you use a CanvasGroup component to hide your menu, make sure you also uncheck the 'Blocks Raycast' when you hide it. (otherise Unity will tell yo that you are hovering the invisible UI ;)

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 richyrich · Nov 02, 2014 at 01:15 PM 0
Share

@troien - do you happen to know of the link in the scripting guide to this function please? $$anonymous$$y system says that function does not exist. I looked in the scripting pages and could not find it listed either. The best I could find was this (http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventSystem.html) which is obviously not right

avatar image troien · Nov 02, 2014 at 02:56 PM 0
Share

Nope, I can´t. The Unity scripting reference is not up to date, and does not contain this. However, the autocomplete of visual studio/monodevelop tells me this method exists and this method works for me. Which version of Unity are you currently using? (I'm using 4.6.0b20)

avatar image KingRecycle · Nov 02, 2014 at 05:17 PM 3
Share

This worked.

After a little searching you want to add -1 to check for mouse.

 UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(-1)
avatar image richyrich · Nov 02, 2014 at 05:21 PM 0
Share

$$anonymous$$y version is 4.5.0f6. Ah, a new feature, all is well - I'd assumed that I'd made some import mistake. I've now found the beta script guide link, but it still does not have the function - but as you say, its not up to date. Thanks for reply :)

avatar image itsharshdeep · Jan 14, 2015 at 11:29 AM 0
Share

Thanks a lot :) It Worked for me

Show more comments
avatar image
4

Answer by Kiwasi · Dec 26, 2014 at 04:33 AM

Also worth noting that you can abandon your own ray casting solution and use the Unity event system with a physics raycaster. You can them implement the various interfaces from UnityEngine.EventsSystem.

Note that the event system is also intended to replace SendMessage.

Edit: Adding a YouTube video, since this question has been asked a few times

Comment
Add comment · Show 2 · 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 sagiv_bg · May 07, 2016 at 02:39 PM 0
Share

hi, looks nice but I'm confused, what if i have a Gamemanager that handle a start game click (basicly any key on any spot on the screen). where do i need to implement the interface then (i've tried to implement it on that Game$$anonymous$$anager but no luck, the OnClick function doesnt fire at all). thanks in advanced

avatar image Nananaaa · Oct 17, 2016 at 07:47 PM 0
Share

What a comprehensive answer the youtube video is. Thank you for sharing this!

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

9 People are following this question.

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

Related Questions

Raycast Target does not block mouse click 1 Answer

UI element in 3D space, reacts to click despite raycast target being infront 0 Answers

Follow Nearest Target 1 Answer

UI Mask for 3d Objects 0 Answers

Look at enemy 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