# Raycast to An UI Object

**URL:** <https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547>\
**Category:** Unity Development\
**Tags:** unity\
**Created:** [September 13, 2023, 12:06am UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547 "2023-09-13T00:06:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhhqu0305](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhhqu0305](https://forum.magicleap.cloud/u/zhhqu0305)\
**Post date:** [September 13, 2023, 12:06am UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/1 "2023-09-13T00:06:50Z")

</div>

Give us as much detail as possible regarding the issue you're experiencing.

**Unity Editor version** : 2022.3.6f1  
**ML2 OS version** :1.4.0-dev1  
**MLSDK version** : 1.4.0-dev1  
**Host OS** : (Windows/MacOS): Windows

I'm trying to do a raycast of the eye gaze to gameobjects in the scene. For 3D objects, the following code works well:

```auto
var leftEyePosition = eyes.leftEyePosition;
var leftEyeRotation = eyes.leftEyeRotation;
var leftEyeGazeDirection = leftEyeRotation * Vector3.forward;
var leftEyeRay = new Ray(leftEyePosition, leftEyeGazeDirection);
if (Physics.Raycast(leftEyeRay, out RaycastHit leftEyeRaycastHit, 100))
{
    // hit something
    var hitPoint = leftEyeRaycastHit.point;
    var _rayLength = leftEyeRaycastHit.distance;
    var gameObject = leftEyeRaycastHit.collider.gameObject;
}

```

I could find the gameobject just fine. But when it comes to 2D UI objects, I could not find any game object. I'm using canvas -- see the figure below for my UI structure:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/magicleap1/original/2X/3/36247ed642b582c06830d423e8a953feda25e86b.png)  
As some posts pointed out, I added a `mesh collider` to my UI objects and it didn't work. I tried adding `mesh renderer` and setting `Cull Transparent Mesh` in `Canvas Render` to false (not sure if it's helpful) but still cannot raycast to it. I tried to look at the examples but seems that none of them uses Canvas -- Is it bad practice to use Canvas for ML2 development? Is so, what should I do and is there a general guideline for it? If I could still use Canvas, is there a way for me to hit these UIs? [This post](https://stackoverflow.com/questions/59463547/how-do-i-make-raycast-hit-with-a-canvas-elementscreen-space-overlay) said something about casting ray to 2D UIs, but I don't think a similar `EventSystem` exists here.

Any help would be appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [September 13, 2023, 4:19am UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/2 "2023-09-13T04:19:12Z")

</div>

The Magic Leap SDK does not include any additional APIs to raycast against Unity UI elements, but you might be able to get more information by testing your scripts logic further. For example, you could try to raycast from the controller or from a specified transform, then test to see if the raycast hits the UI element. I suspect that the **raycast does not return the result properly because your UI elements do not having a collider**.

In the future, I recommend searching other Unity resources to find answers more quickly, since the issue is not Magic Leap specific: [https://discussions.unity.com/](https://discussions.unity.com/)

I also recommend the following resources that could make development easier:

MRTK 3 allows you to interact with UI elements using your hands, eyes and controller :[Mixed Reality ToolKit 3 Overview | MagicLeap Developer Documentation](https://developer-docs.magicleap.cloud/docs/guides/third-party/mrtk3/mrtk3-overview/)

Unity Created a learn guide on selecting objects with your eyes using XRI, which can be used to select UI elements if the functionality is extended further:

> **[Eye tracking - Unity Learn](https://learn.unity.com/tutorial/eye-tracking?uv=2022.2&courseId=643419cbedbc2a5f42275126)**
>
> Eye tracking is a feature of the Magic Leap 2 that allows you to track the user’s eye movements, blinks, fixation point, and other data. Using this information, you can make improvements to the user experience, such as activating or highlighting an...

---

<div class="post-metadata">

**Author:** ![zhhqu0305](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhhqu0305](https://forum.magicleap.cloud/u/zhhqu0305)\
**Post date:** [September 13, 2023, 4:25am UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/3 "2023-09-13T04:25:54Z")

</div>

Thanks for your answer! I did add the collider but it's not working properly. I would experiment with the `XR Rig` object and see if that solves my problem. Thanks!

---

<div class="post-metadata">

**Author:** ![yanghouze](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/yanghouze/32/1470_2.png) [@yanghouze](https://forum.magicleap.cloud/u/yanghouze)\
**Post date:** [February 2, 2024, 2:40pm UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/4 "2024-02-02T14:40:17Z")

</div>

Hi, have you solved this problem? I am also have the same error. For normal objects, it works but for ui element, it not.

---

<div class="post-metadata">

**Author:** ![zhhqu0305](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhhqu0305](https://forum.magicleap.cloud/u/zhhqu0305)\
**Post date:** [February 2, 2024, 4:28pm UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/5 "2024-02-02T16:28:33Z")

</div>

I have to set the `render mode` of Canvas to `world space` and then it works

---

<div class="post-metadata">

**Author:** ![yanghouze](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/yanghouze/32/1470_2.png) [@yanghouze](https://forum.magicleap.cloud/u/yanghouze)\
**Post date:** [February 2, 2024, 8:30pm UTC](https://forum.magicleap.cloud/t/raycast-to-an-ui-object/2547/6 "2024-02-02T20:30:06Z")

</div>

Hi, are you using 3d colliders or 2d ones. I am used to work with 2d and it doesn't work. But when I change it to 3d, it works.
