Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question InputTestFixture error

Discussion in 'Input System' started by glissanto, Apr 24, 2023.

  1. glissanto

    glissanto

    Joined:
    Apr 6, 2023
    Posts:
    2
    I'm using Unity 2022.2.16f1 (Input System 1.5.1) and keep encountering this error when trying to run my play mode tests (they ran fine on Unity 2021).

    In my component, where "PlayerInput" is the class generated by my input actions:

    Code (CSharp):
    1. private PlayerInput controls;
    2.  
    3. // ...
    4.  
    5.     void Awake() {
    6.        controls = new PlayerInput();
    7.        controls.Character.Interact.performed += ctx => OnInteract();
    8.        controls.Character.Move.performed += StartMoving;
    9.        controls.Character.Move.canceled += (ctx) => _mover.StopMoving();
    10.        controls.Character.Run.performed += ctx => _mover.StartRunning();
    11.        controls.Character.Run.canceled += ctx => _mover.StopRunning();
    12.     }
    13.  
    14.     void OnEnable() {
    15.         controls.Enable();
    16.     }
    17.  
    18.     void OnDisable() {
    19.         controls.Disable();
    20.     }
    In my test, which inherits InputTestFixture:

    Code (CSharp):
    1.     public override void Setup() {
    2.         base.Setup();
    3.         keyboard = InputSystem.AddDevice<Keyboard>();
    4.  
    5.         playerObj = GameObject.Find("Player");
    6.         if (playerObj != null) {
    7.             // This is (a hacky?) way to get the player inputs
    8.             // to bind to the new keyboard input device
    9.             // (because it calls `controls.Enable()`).
    10.             var playerController = playerObj.GetComponent<PlayerController>();
    11.             playerController.enabled = false;
    12.             playerController.enabled = true;
    13.         }
    14.     }
    15.  
    Code (csharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. UnityEngine.InputSystem.InputManager.AddStateChangeMonitor (UnityEngine.InputSystem.InputControl control, UnityEngine.InputSystem.LowLevel.IInputStateChangeMonitor monitor, System.Int64 monitorIndex, System.UInt32 groupIndex) (at ./Library/PackageCache/com.unity.inputsystem@1.5.1/InputSystem/InputManagerStateMonitors.cs:31)
    From calling "controls.Enable()".

    Did something change in Unity 2022 about how the InputTestFixture works, or is this a bug?
     
  2. glissanto

    glissanto

    Joined:
    Apr 6, 2023
    Posts:
    2
    So I tried a few things, and changing my component to setup the controls like this works (moving the setup from "Awake" to "OnEnable"):

    Code (CSharp):
    1.     void OnEnable() {
    2.         if (controls == null) {
    3.             controls = new PlayerInput();
    4.             controls.Character.Interact.performed += ctx => OnInteract();
    5.             controls.Character.Move.performed += StartMoving;
    6.             controls.Character.Move.canceled += (ctx) => _mover.StopMoving();
    7.             controls.Character.Run.performed += ctx => _mover.StartRunning();
    8.             controls.Character.Run.canceled += ctx => _mover.StopRunning();
    9.         } else {
    10.             controls.Enable();
    11.         }
    12.     }
    13.  
    14.     void OnDisable() {
    15.         controls.Disable();
    16.     }
    17.  
    No idea why this one works and the other one doesn't!