1

Is there way to call the onclick event from a raycast? I have world scale canvas attached to an object that has images with buttons. When I select the button with the mouse my event function is called. But now I am trying to adjust the code so I can avoid using mouse all together.

[SerializeField]
private Image cursor;
[SerializeField]
private LayerMask uI;

if (Input.GetButtonDown("Fire1"))
    {
        Ray ray = Camera.main.ScreenPointToRay(cursor.transform.position);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 100,uI))
        {
            Debug.Log(hit.collider); 
        }
    }      

This is the code I use to raycast into the world space canvas which works and returns the images but I am not sure how I can now call the onclick event since I am not using the mouse but a image instead

Is there way to call the onclick event attached to image that I raycast to or do I have to redo my entire script and not use on click events?

Galandil
  • 4,169
  • 1
  • 13
  • 24
snake555510
  • 81
  • 1
  • 1
  • 11
  • The title and the body o your questions seems to be saying different stuff. You just want to detect click on a UI Image? If that's what you are trying to do(I suspect) then see the duplicate. If not, please let me know and explain your question – Programmer Mar 16 '18 at 18:13

2 Answers2

2

Hen you hit a GameObject, get its list of components that implement OnPointerClick event

IPointerClickHandler clickHandler=collider.gameObject.GetComponent<IPointerClickHandler>();

once you have that reference (its not null) you can call

PointerEventData pointerEventData=new PointerEventData(EventSystem.current);
clickHandler.OnPointerClick(pointerEventData)

That should do the job

zambari
  • 4,797
  • 1
  • 12
  • 22
1

I believe that the Button component does what you seek as well.

But to answer your question about raycasts.

Is there way to call the onclick event from a raycast?

//Example raycast code
//Variables
public RayCastHit _Hit;
public LayerMask _RaycastCollidableLayers; //Set this in inspector, makes you able to say which layers should be collided with and which not.
public float _CheckDistance = 100f;

//Method
void PerformRaycast(){
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Physics.Raycast(ray, out _Hit, _CheckDistance + 0.1f, _RaycastCollidableLayers);
    if (_Hit.collider == null)
    {
        Debug.Log("Raycast hit nothing");
        return null;
    }
    GameObject go = _Hit.collider.gameObject;
}

Your raycast hits an object and stores a reference to that object. As you can see in my example you can fetch the game object through the RaycastHit.

When you have the game object you can access any scripts on it through GetComponent<>(), this means you can say.

GameObject go = _Hit.collider.gameObject;
OnClickScript ocs = go.GetComponent<OnClickScript>();
ocs.OnClick();

If I misunderstood your query, please let me know.

Hexodus
  • 12,361
  • 6
  • 53
  • 72
Doh09
  • 2,324
  • 1
  • 16
  • 31