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