Search Unity

Bug Input state not checked when getting focus

Discussion in 'Input System' started by khan-amil, Jun 3, 2022.

  1. khan-amil

    khan-amil

    Joined:
    Mar 29, 2012
    Posts:
    206
    Despite enabling the "initial state check" on my input action, if I get focus to Unity with the key corresponding to my action held, the started event is called only when releasing the key.

    It's an issue as I was using this action for a key that we need to hold, so if you focus back with the key held the behavior is inverted.
    Here's a basic example of the code I'm using, am I missing something or is it indeed a bug ?

    Code (CSharp):
    1.     public bool _buttonHeld;
    2.     public GameObject _feedback;
    3.    
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.        
    8.         TestActions actions = new TestActions();
    9.         actions.Default.ButtonHold.started += ctx =>
    10.         {
    11.             Debug.Log("started : "+ctx.ReadValueAsButton());
    12.             ToggleMode(ctx.ReadValueAsButton());
    13.         };
    14.         actions.Default.ButtonHold.canceled += ctx =>
    15.         {
    16.             Debug.Log("canceled : "+ctx.ReadValueAsButton());
    17.             ToggleMode(ctx.ReadValueAsButton());
    18.         };
    19.         actions.Enable();
    20.     }
    21.  
    22.     private void ToggleMode(bool state)
    23.     {
    24.         _buttonHeld = state;
    25.         _feedback.SetActive(state);
    26.     }