Unity Eye Tracking On gameObject to change color

Hi, I am relatively new to unity and ML2 along with working with c#. I've been just messing around with the unity the last couple days, and have been attempting to implement a feature in my app where one can change the color of a sphere, by focusing on it. I've used these as my references:

Eye Tracking - Unity | Magic Leap

Eye Tracking | MagicLeap Developer Documentation

I've tried to combine aspects of both code, while keeping in mind that the API has changed for eye tracking between ML1 and ML2. I've built my app and focused on a sphere, however the colors won't change as expected. I'd appreciate any sort of guidance. Thanks! My code is below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using static UnityEngine.XR.MagicLeap.InputSubsystem.Extensions;
//adding my own

using UnityEngine.InputSystem;
using UnityEngine.UI;
using UnityEngine.XR;
using InputDevice = UnityEngine.XR.InputDevice;
using UnityEditor;
using static MagicLeapInputs;
using UnityEngine.Android;

public class EyeTracking : MonoBehaviour


public GameObject Camera;
public Material FocusedMaterial, NonFocusedMaterial;

private Vector3 _heading;
private MeshRenderer _meshRenderer;

//ml inputs storage variable
private MagicLeapInputs mlInputs;
//eye actions storage variable
private MagicLeapInputs.EyesActions eyesActions;
// other eye data storage variables
private InputDevice eyesDevice;

// Was EyeTracking permission granted by user
private bool permissionGranted = false;
private readonly MLPermissions.Callbacks permissionCallbacks = new MLPermissions.Callbacks();

private void Awake()
    permissionCallbacks.OnPermissionGranted += OnPermissionGranted;
    permissionCallbacks.OnPermissionDenied += OnPermissionDenied;
    permissionCallbacks.OnPermissionDeniedAndDontAskAgain += OnPermissionDenied;

void Start()
    mlInputs = new MagicLeapInputs();
    MLPermissions.RequestPermission(MLPermission.EyeTracking, permissionCallbacks);

    transform.position = Camera.transform.position + Camera.transform.forward * 2.0f;
    _meshRenderer= gameObject.GetComponent<MeshRenderer>();


private void OnDestroy()



private void Update()


    if (!permissionGranted)

    if (!eyesDevice.isValid)
        this.eyesDevice = InputSubsystem.Utils.FindMagicLeapDevice(InputDeviceCharacteristics.EyeTracking | InputDeviceCharacteristics.TrackedDevice);

    var eyes = eyesActions.Data.ReadValue<UnityEngine.InputSystem.XR.Eyes>();

        RaycastHit rayHit;
        _heading = eyes.fixationPoint - Camera.transform.position;
        // Use the proper material
        if (Physics.Raycast(Camera.transform.position, _heading, out rayHit, 10.0f))
            _meshRenderer.material = FocusedMaterial;
            _meshRenderer.material = NonFocusedMaterial;


private void OnPermissionDenied(string permission)
    MLPluginLog.Error($"{permission} denied, example won't function.");
private void OnPermissionGranted(string permission)
    eyesActions = new MagicLeapInputs.EyesActions(mlInputs);
    permissionGranted = true;


Hi @mihir.shah,

Welcome to the Magic Leap 2 Developer forums. We are so happy to have you here engaging with us for the first time.

In order to help you in the best way possible, we need a few more details before we move forward. Could you please provide the following?

  • Unity Editor version
  • ML2 OS Version
  • ML SDK Version
  • Development OS Platform

This will help us further diagnose the problem. Also, when running this code in the editor, are you able to manually rotate the headset in the scene view and have the spheres change color?



• Unity Editor version : 2022.30f1
• ML2 OS Version : 1.3.0-dev1
• ML SDK Version: 1.3.0-dev1
• Development OS Platform: Android (I think, not sure what you mean by this)

No I'm not able to manually rotate the headset in the scene view and have the spheres change colo.r

Are you drawing/visualizing the ray coming from the eyes to make sure it is pointing the right direction in play mode? This will help us isolate any problems that may be present when running it on the headset.

Apologies for the confusion. Are you developing on Mac, Windows, etc?

No worries, I'm developing on windows!

I don't think I've drawn in the ray coming in from the eyes, I've relied on the logic provided by the ML1 code here: Developer Portal | Magic Leap

I haven't really manipulated the "Eyes Controller" in the XR Rig, if that is what you are referring to.

Also I just noticed I receive the following error when I am in the play mode:

Error: MLZIPermissionsIsGranted in the Magic Leap API failed. Reason: MLResult_UnspecifiedFailure
UnityEngine.XR.MagicLeap.MLDevice:Update () (at ./Library/PackageCache/com.magicleap.unitysdk@1.7.0/Runtime/Common/Utils/MLDevice.cs:448)

Have you granted eye tracking permissions in the ML hub for the application simulator?

Yes, the error has now disappeared. And the functionality now seems to work in the application simulator, but doesn't work when I build it and run it in ML2.

Are you seeing any new errors when you run it on the ML2?

I am currently also seeing this same error when trying to run the simulator with the device. I am stuck having to Build to view my code changes.

Does the eye tracking example scene work in the simulator for you? @x11