Search Unity

Bug m_SelectEnterEventArgs change if SelectEnter() get's called OnSelectEntered()

Discussion in 'XR Interaction Toolkit and Input' started by MarekLg, Jul 14, 2021.

  1. MarekLg

    MarekLg

    Joined:
    Jan 31, 2018
    Posts:
    26
    Suppose we have this Setup:

    Code (CSharp):
    1. public class MyInteractor : XRBaseInteractor
    2. {
    3.     private MyInteractable myInteractable;
    4.  
    5.     // ...
    6.  
    7.     protected override void Awake()
    8.     {
    9.         base.Awake();
    10.  
    11.         myInteractable.selectEntered.AddListener(_ =>
    12.         {
    13.             interactionManager.SelectEnter(this, myOtherInteractable);
    14.         });
    15.     }
    16.  
    17.     // ...
    18. }
    19.  
    20. public class MyInteractable : XRBaseInteractable
    21. {
    22.    // ...
    23.  
    24.     protected override void OnSelectEntered(SelectEnterEventArgs args)
    25.     {
    26.         base.OnSelectEntered(args);
    27.  
    28.         // Do something with args.interactor
    29.     }
    30.  
    31.    // ...
    32. }
    33.  
    This happens if myInteractable has the same interactionManager as myInteractor.
    After line 26 args.interactor has changed to the interactor of the selection entered in line 13.

    The expected behaviour would be that the interactor of the selection happening in line 24 stays the same, even when another selection is entered during OnSelectionEntered().
     
    ajibadequwiyu likes this.
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    They reuse the same event args instance for performance reasons (to avoid allocations etc.) as a workaround you can implement your own interaction manager and override the virtual methods like SelectEnter to either create a new event args instance for every call or pool them to get a instance from the pool per call and return it after the call instead of reusing the same args instance for all calls

    You could make a bug report inside Unity if you want this to be changed, because otherwise they might miss it in the forums
     
    chris-massie likes this.
  3. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    231
    There isn't a public issue tracker for this bug, but we are aware of this issue and it will be fixed in a future version. R1PFake describes the underlying problem and the workaround, and we'll be adding an object pool to the Interaction Manager to fix this issue.