Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I met an error when I disable an InputControls.

Discussion in 'Input System' started by watsonsong, Jun 26, 2018.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I always met this problem when call the Disable() in OnDisable:
    Code (CSharp):
    1.  UnityEngine.Experimental.Input.LowLevel.IInputStateChangeMonitor monitor, System.Int64 monitorIndex) (at Packages/com.unity.inputsystem/InputSystem/InputManager.cs:991)
    2. UnityEngine.Experimental.Input.InputActionMapState.DisableControls (System.Int32 mapIndex, System.Int32 controlStartIndex, System.Int32 numControls) (at Packages/com.unity.inputsystem/InputSystem/Actions/InputActionMapState.cs:361)
    3. UnityEngine.Experimental.Input.InputActionMapState.DisableAllActions (UnityEngine.Experimental.Input.InputActionMap map) (at Packages/com.unity.inputsystem/InputSystem/Actions/InputActionMapState.cs:281)
    4. UnityEngine.Experimental.Input.InputActionMap.Disable () (at Packages/com.unity.inputsystem/InputSystem/Actions/InputActionMap.cs:177)
    5. UnityEngine.Experimental.Input.InputActionWrapper.Disable () (at Packages/com.unity.inputsystem/InputSystem/Actions/InputActionWrapper.cs:31)
    6. Game.RoleController.OnDisable () (at Assets/Game/Scripts/Role/RoleController.cs:317)
     
  2. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Well, the error happend here:
    Code (CSharp):
    1. public void Remove(IInputStateChangeMonitor monitor, long monitorIndex)
    2.             {
    3.                 if (listeners == null)
    4.                     return;
    5.  
    6.                 ////REVIEW: would be better to clean these up implicitly during the next traversal
    7.                 for (var i = 0; i < listeners.Length; ++i)
    8.                     if (ReferenceEquals(listeners[i].monitor, monitor) && listeners[i].monitorIndex == monitorIndex)
    9.                     {
    10.                         ArrayHelpers.EraseAt(ref listeners, i);
    11.                         ArrayHelpers.EraseAt(ref memoryRegions, i);
    12.                         signalled.SetLength(signalled.length - 1);
    13.                     }
    14.             }
    In the loop the listeners may be null, I add this check to avoid this error, but maybe not a good way.

    Code (CSharp):
    1. public void Remove(IInputStateChangeMonitor monitor, long monitorIndex)
    2.             {
    3.                 if (listeners == null)
    4.                     return;
    5.  
    6.                 ////REVIEW: would be better to clean these up implicitly during the next traversal
    7.                 for (var i = 0; listeners != null && i < listeners.Length; ++i)
    8.                     if (ReferenceEquals(listeners[i].monitor, monitor) && listeners[i].monitorIndex == monitorIndex)
    9.                     {
    10.                         ArrayHelpers.EraseAt(ref listeners, i);
    11.                         ArrayHelpers.EraseAt(ref memoryRegions, i);
    12.                         signalled.SetLength(signalled.length - 1);
    13.                     }
    14.             }
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779