- Home /
This question was
closed May 08, 2018 at 10:17 AM by
keckluis for the following reason:
The question is answered, right answer was accepted
How to detect UI Elements with Raycast
Hey guys, so I have an image I use as a cursor and I want to get the name of a UI element, when the cursor hovers of the element. This is what I have at the moment:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class abc : MonoBehaviour {
GameObject _Cursor;
RaycastHit hit;
void Start () {
_Cursor = GameObject.Find("CursorNormal");
}
void Update () {
if (Physics.Raycast (Camera.main.ScreenPointToRay (_Cursor.transform.position), out hit, Mathf.Infinity)) {
print(hit.collider.name);
}
}
}
This script prints the name of my 3D models, but it doesn't work with UI elements. Do you know how I can make it work?
Comment
Best Answer
Answer by anthot4 · May 08, 2018 at 10:04 AM
You have to do a seperate type of raycast for ui elements called a graphics raycast. For more info see: https://docs.unity3d.com/ScriptReference/UI.GraphicRaycaster.Raycast.html