Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Can't disable actions

Discussion in 'Input System' started by SMT5015, Sep 14, 2021.

  1. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    I need to temporary restrict player's control of the character, for stagger, cutscenes or whatever. But after some test I noticed that Input system's methods for it just have no effect at all.

    I pass input through events and use code generation feature to manage actions. So my disable functions look like this:
    Code (CSharp):
    1. public void EnableControls(bool enable)
    2.     {  
    3.         if(enable)
    4.         {
    5.             customClassField.ActionMap.Enable();
    6.         }
    7.         else
    8.         {
    9.             customClassField.ActionMap.Disable();
    10.         }
    11.     }
    Code (CSharp):
    1. public void BanCertainActions(bool ban)
    2.     {
    3.         if(restrict)
    4.         {
    5.             customClassField.ActionMap.Action1.Disable();
    6.             customClassField.ActionMap.Action2.Disable();
    7.             customClassField.ActionMap.Action3.Disable();
    8.         }
    9.         else
    10.         {
    11.             customClassField.ActionMap.Action1.Enable();
    12.             customClassField.ActionMap.Action2.Enable();
    13.             customClassField.ActionMap.Action3.Enable();
    14.         }
    15.     }
    And they were used like
    Code (CSharp):
    1. public IEnumerator UncontrolledMove()
    2. {
    3.       //whatever stuff it does
    4.       EnableControls(false);
    5.  
    6.       yield return reqiredTime;
    7.       //whatever else happens here
    8.       EnableControls(true);
    9. }
    It did everything except disabling controls, and when this method was called by whatever input action I laid my eyes upon in the script it still did nothing. Where is the problem? Can it be in some conditions I didn't mention here?

    P.S. Separate methods for enabling and disabling look cumbersome.
     
  2. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Did some testing. Turns out that actions still work despite being disabled in the generated script. Guess it is because I'm using it and the PlayerInput component at the same time. Am I right?

    Also, the generated class instance is private (and not static) for the script with all functions which are subscribed to the events, and disable-enable ones are in there too. Soes it matter?