Search Unity

Direct interactor of XR Interaction Toolkit throwing exceptions

Discussion in 'XR Interaction Toolkit and Input' started by TonyVT, Jun 27, 2020.

  1. TonyVT

    TonyVT

    Joined:
    Sep 11, 2014
    Posts:
    13
    Hello everyone! I'm just signaling a problem that I've found while using the XR Interaction toolkit, so that to say that there is a problem, and also to propose a hack if you stumble here from Google.

    Working with the XR toolkit, I have found a bug in the code. If you are using the direct interactor, if you delete some objects that are close to it and are colliding with it, it may throw an exception. This may also happen if you destroy an object that you had in your hand and that you just launched. The exception says that you are still trying to access Collider even if the object has been destroyed.

    The error depends on how the direct interactor handles the possible objects it may interact with. To solve it, you have to modify the code of XRDirectInteractor.cs and substitute the function GetValidTargets (around line 62 of the file) with this one that checks for null


    Code (CSharp):
    1.         /// <summary>
    2.         /// Retrieve the list of interactables that this interactor could possibly interact with this frame.
    3.         /// This list is sorted by priority (in this case distance).
    4.         /// </summary>
    5.         /// <param name="validTargets">Populated List of interactables that are valid for selection or hover.</param>
    6.         public override void GetValidTargets(List<XRBaseInteractable> validTargets)
    7.         {
    8.             validTargets.Clear();
    9.             m_InteractableDistanceSqrMap.Clear();
    10.  
    11.             // Calculate distance squared to interactor's attach transform and add to validTargets (which is sorted before returning)
    12.             for (int i = m_ValidTargets.Count - 1; i >= 0; i--)
    13.             {
    14.                 var interactable = m_ValidTargets[i];
    15.  
    16.                 if (interactable == null)
    17.                 {
    18.                     m_ValidTargets.Remove(interactable);
    19.                     continue;
    20.                 }
    21.                 m_InteractableDistanceSqrMap[interactable] = interactable.GetDistanceSqrToInteractor(this);
    22.                 validTargets.Add(interactable);
    23.             }
    24.  
    25.             validTargets.Sort(m_InteractableSortComparison);
    26.         }
    If the code works for you, look online how to modify the code from packages, or the next time you reboot Unity the fix will vanish away!

    I hope to have helped someone, have a nice day :)
     
    Cheshire96, khnivlam and shidihun like this.
  2. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    755
    I'm running into this issue as well. Any plans to fix it, Unity?
     
    khnivlam likes this.