Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Input actions not triggered?

Discussion in 'Input System' started by TWolfram, Nov 7, 2019.

  1. TWolfram

    TWolfram

    Joined:
    Aug 6, 2017
    Posts:
    75
    For some reason, my input actions stopped triggering. The code used to work(kind of; abilityAction threw an exception whenever I would press the assigned button, but directionAction worked as expected), but IIRC I changed nothing about the input system or the class that used it, and it seemed to just break from one day to the other. This is my code:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. [CreateAssetMenu(menuName = "Player/BindingsInput")]
    6. public class BindingsInput : IPlayerInput
    7. {
    8.     public InputAction directionAction;
    9.     public InputAction abilityAction;
    10.  
    11.     private PlayerInputData _playerInput;
    12.     public override void Disable(){
    13.         directionAction.Disable();
    14.         abilityAction.Disable();
    15.         directionAction.performed -= (x) => _playerInput.direction = x.ReadValue<Vector2>();
    16.         directionAction.canceled -= (x) => _playerInput.direction = x.ReadValue<Vector2>();
    17.         abilityAction.performed -= (x) => _playerInput.Ability = x.ReadValue<bool>();
    18.     }
    19.  
    20.     public override void Enable()
    21.     {
    22.         directionAction.Enable();
    23.         abilityAction.Enable();
    24.         directionAction.performed += UpdateDirectionalInput();
    25.         directionAction.canceled += (x) => _playerInput.direction = x.ReadValue<Vector2>();
    26.         abilityAction.performed += (x) => _playerInput.Ability = x.ReadValue<bool>();
    27.     }
    28.  
    29.     private Action<InputAction.CallbackContext> UpdateDirectionalInput()
    30.     {
    31.         return (x) => {
    32.             _playerInput.direction = x.ReadValue<Vector2>();
    33.         };
    34.     }
    35.  
    36.     public override Vector2 GetDirectionalInput() =>_playerInput.direction;
    37.  
    38.     public override PlayerInputData GetPlayerInput() => _playerInput;
    39. }
    The "UpdateDirectionalInput()" method used to be an inline lambda like the other actions, but I made it a method to try to debug it. However, it seems like the breakpoint doesn't get reached at all, which makes me think the actions "performed" and "canceled" just don't get triggered for some reason. I tried to add another class to approach it in a different way:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. [CreateAssetMenu(menuName = "Player/UpdatedBindingsInput")]
    6. public class UpdatedBindingsInput : IPlayerInput
    7. {
    8.     public InputAction directionAction;
    9.     public InputAction abilityAction;
    10.  
    11.     private PlayerInputData _playerInput;
    12.     public override void Disable(){
    13.         directionAction.Disable();
    14.         abilityAction.Disable();
    15.     }
    16.  
    17.     public override void Enable()
    18.     {
    19.         directionAction.Enable();
    20.         abilityAction.Enable();
    21.     }
    22.  
    23.     public override Vector2 GetDirectionalInput() => directionAction.ReadValue<Vector2>();
    24.  
    25.     public override PlayerInputData GetPlayerInput() => new PlayerInputData(abilityAction.ReadValue<bool>(), GetDirectionalInput());
    26. }
    However, this doesn't work either.

    Does anyone have a clue as to how this could have happened? I already checked if the "Enable()" method gets called as expected, and it did.