Get Name of UI gameobject with raycast

I wont the name of UI gameobject with help of Raycast.

For example if my finger is on any button with name “Cartoon” then i need that name with help of raycast.

Physics.Raycast doesn’t work on a Canvas Graphic but a GraphicRaycaster does.

You can fire one off manually using

myGRaycaster.Raycast(data, rayResults);

where myGRaycaster is your Graphics Raycaster component on your Canvas, data is PointerEventData and rayResults is a List.<RaycastResult>.

Using the pointer event data supplied the GRaycaster will find all the UI elements touched by the ray and will append them all to the List you supplied.

However, I discovered this can be a needless step if you have included the Pointer Event interfaces in your script.

For example, if you have implemented OnPointerDown or OnDrag etc you can use the PointerEventData populated in that Method to obtain the Raycast information associated with that event without performing an extra raycast.

#pragma strict

import UnityEngine.UI;
import UnityEngine.EventSystems;

public class FiringAGraphicsRay extends MonoBehaviour implements IPointerDownHandler
{
	function OnPointerDown(data : PointerEventData)
	{
		Debug.Log(data.pointerCurrentRaycast.gameObject.name);
	}
}

The key here is pointerCurrentRaycast member of the PointerEventData which is generated in our interface.

you can use raycasthit object to get what was hit.

The example of how to use this is at the bottom of this page.

i know this answer is a bit scarce but I do want to show people HOW to find this info just so they know how to resolve another issue next time.

fyi: the “out” keyword basically fills in that object from within the method called. so out hit gives you a plethora of info regarding what was hit. that you can utilise after the method has been called.
May be a good idea to put an if statement on it like if(hit != null) just to make sure that you actually hit something otherwise referencing a property of hit would fire an exception.

I hope this helps, if you need further help, just ask. (make sure you use @Zeoli so i get an email that you mentioned me).

Thanks, this is the answer why vr samples does not work with a canvas ui menu, any idea how to make the menu buttons function wiht a collision as a mouse0 would ? Thanks