XRDirectInteractor - how to determine which object is available for taking?

Good afternoon!
Tell me how to determine the closest object to the “hands” with XRDirectInteractor, which will be taken when the Grab button is pressed on the controller?

I made sure that the nearest object to the “hand” was highlighted with XRDirectInteractor on it, but even though the nearest object is highlighted, it is still not he who is taken into his hands… How exactly does XRDirectInteractor determine the object to be taken? Does he have any priorities? I can’t understand and solve the problem…

I create an array of objects with the “Target” tag, then determine the closest object to the XRDirectInteractor in OnTriggerStay and change the shader parameter for highlighting. But either the nearest object is not determined, or I can’t understand at all how XRDirectInteractor still decides which object will be taken? I just need the correct object to be highlighted, which will be taken after pressing the capture button on the controller, but the incorrect weight is highlighted, or if the nearest one is highlighted, then why is another object taken by the controller, please tell me… The code example is below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OutlineNiarestTargetSelect : MonoBehaviour
{
    GameObject[] ArrayOfInteractiveObjects;
    GameObject NiarestInteractiveObject;
    void Start()
    {
        ArrayOfInteractiveObjects = GameObject.FindGameObjectsWithTag("Target");
    }
    void OnTriggerExit(Collider other)
    {
        other.gameObject.GetComponent<MeshRenderer>().materials[1].SetFloat("_Scale", 1.0f);
    }
    void OnTriggerStay(Collider other)
    {
        FindNiarestTarget().GetComponent<MeshRenderer>().materials[1].SetFloat("_Scale", 1.015f);
    }
    GameObject FindNiarestTarget()
    {
        float distance = Mathf.Infinity;
        Vector3 HandPosition = transform.position;
        foreach (GameObject go in ArrayOfInteractiveObjects)
        {
            Vector3 diff = go.transform.position - HandPosition;
            go.GetComponent<MeshRenderer>().materials[1].SetFloat("_Scale", 1.0f);
            float curDistance = diff.sqrMagnitude;
            if(curDistance < distance)
            {
                NiarestInteractiveObject = go;
                distance = curDistance;
            }
        }
        return NiarestInteractiveObject;
    }
}

I just had the same issue. Here is what I found:

The XRInteractionManager has a function which can give you valid targets, sorted by priority.
https://docs.unity3d.com/Packages/com.unity.xr.interaction.toolkit@3.0/api/UnityEngine.XR.Interaction.Toolkit.XRInteractionManager.GetValidTargets.html

You can use it like this and use validTargets[0] to get the object that will be selected in case of a grab action:

 List<IXRInteractable> validTargets = new List<IXRInteractable>();
        interactionManager.GetValidTargets(interactor.GetComponent<IXRInteractor>(), validTargets);

Currently I am using this script to activate the outline.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

/// <summary>
/// put script twice(!) on Interaction Manager (one time for each hand).
/// </summary>
public class OutlineIfCanBeSelected : MonoBehaviour
{
    [SerializeField] private XRDirectInteractor interactor;

    private XRInteractionManager interactionManager;

    private Outline lastOutline;

    void Start()
    {
        interactionManager = transform.GetComponent<XRInteractionManager>();
    }

    void Update()
    {
        ShowOutlineIfCanBeSelected();
    }

    public void ShowOutlineIfCanBeSelected()
    {
        if (lastOutline != null)
        {
            lastOutline.enabled = false;
        }

        List<IXRInteractable> validTargets = new List<IXRInteractable>();
        interactionManager.GetValidTargets(interactor.GetComponent<IXRInteractor>(), validTargets);
        if (validTargets.Count > 0)
        {
            lastOutline = validTargets[0].transform.GetComponent<Outline>();
            lastOutline.enabled = true;
        }
        else
        {
            lastOutline = null;
        }
    }
}