• 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
1
Question by DragonautX · Nov 21, 2016 at 06:20 PM · c#raycast

What do I pass into Graphic.Raycast to make it work?

I'm trying to figure out what kind of raycast responds to a UI element with "Raycast Target" checked, like a Raw Image. I found `Graphic.Raycast`, and I want to try it out, but the parameters I'm passing aren't working. The syntax is this:

 public bool Raycast(Vector2 sp, Camera eventCamera);

I tried an if statement in Update(), but I get the error "An object reference is required to access non-static member UnityEngine.UI.Graphic.Raycast(UnityEngine.Vector2, UnityEngine.Camera)".

 if (Graphic.Raycast(new Vector2(20f,20f), Camera.main)) {
     Debug.Log("true");
 } else {
     Debug.Log("false");
 }

What can I input into Graphic.Raycast to make the error go away and make it work? I guess from the error, it has something to do with "object reference", but I don't know how to interpret that.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Nov 21, 2016 at 07:35 PM

You have the wrong picture of the Raycast method of the UI.Graphic class. It's an instance method, not a static method. It could be compared to Collider.Raycast. It only tests the given object against that "ray".

In the case of "Graphic.Raycast" it simply checks if the point is "inside" the rect of the given graphic element. It could be loosely compared with Bounds.Contains. In most cases you don't want / need to use this method manually. It's used by the GraphicRaycaster class to determine if a particular Graphic is hit or not.

So to answer the question "how to make it work", you just need an UI.Graphic element (like an UI.Image or UI.Text) and use this method on a reference to that element to test if the given point is inside the graphic element or not.

 // Example (C#)
 UnityEngine.UI.Image someImageReference;
 
 if (someImageReference.Raycast(new Vector2(20,20), Camera.main))
     Debug.Log("point (20, 20) is inside the image that is referenced by 'someImageReferenc'");

Since you didn't say what kind of problem you want to solve the question should be answered now. If you actually have a problem you want to solve, you might ask a new question and describe your problem.

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 DragonautX · Nov 21, 2016 at 08:00 PM 0
Share

That works, thanks. I wanted to know how to use Graphic.Raycast. I guess I shouldn't use it directly then. It seems I can put any Vector2 in Raycast and it returns True, like Vector2(20,20), even if the rect transform has width and height of 5. The docs say "True if the provided point is a valid location for GraphicRaycaster raycasts". Does the Vector2 really need to be inside the rect transform bounds of an Image?

avatar image Bunny83 DragonautX · Nov 21, 2016 at 09:02 PM 1
Share

Well, the point of Raycast is a bit more complex ^^. It doesn't really do the range check against the RectTransform, that's done by the GraphicRaycaster to deter$$anonymous$$e "potential" targets. What Raycast actually does depends on the object. It simply searches for the interface "ICanvasRaycastFilter". This interface provides a method to "filter out" certain points. If nothing should be filtered it will simply return "true".

For example the UI.Image class implements this interface and transforms the point into texture coordinates. It then reads the pixel at that point to do an alpha check of the returned color in case that image want a pixel-perfect hit detection where transparent areas should be ignored.

See the official source in the bitbucket repo of Unity: Graphic.Raycast and Image.IsRaycastLocationValid which is called from inside the Raycast method in line 500, 503 and 507.

Just in case you're interested, here's the Raycast method of the GraphicRaycaster class. The actual RectTransform check is done inside this static helper method which will fill the list with all potential Graphic objects where the given point is inside.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Raycast from camera not hitting Terrain 1 Answer

Raycast pink material 1 Answer

How to find all colliders below mouse? 2 Answers


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