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.

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.