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

ActionMap.Disable() does not disable actions for all scripts

Discussion in 'Input System' started by schwmm, Mar 20, 2021.

  1. schwmm

    schwmm

    Joined:
    Oct 20, 2020
    Posts:
    62
    Hi,

    currently I'm using the new Input System for my player controls. The actions are grouped into an actionmap, which I access via the auto-generated C# script's namespace. In my player script, I enable and disable the actionmap - this works as expected:

    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         playerActionMap.Enable();
    4.     }
    5.     private void OnDisable()
    6.     {
    7.         playerActionMap.Disable();
    8.     }
    Some of the controls are also being used in other scripts. A simple example can be seen below:

    Code (CSharp):
    1. public class TestOnJumpButton : MonoBehaviour
    2. {
    3.     private Controls controls;
    4.     private InputActionMap playerActionMap;
    5.  
    6.  
    7.     private void Awake()
    8.     {
    9.         controls = new Controls();
    10.         playerActionMap = controls.Player;
    11.  
    12.         controls.Player.Jump.performed += ctx => TestOnInput();
    13.     }
    14.  
    15.     private void OnEnable()
    16.     {
    17.         playerActionMap.Enable();
    18.     }
    19.     private void OnDisable()
    20.     {
    21.         playerActionMap.Disable();
    22.     }
    23.  
    24.     private void TestOnInput()
    25.     {
    26.         Debug.Log("Pressed Jump Button");
    27.    
    28.     }
    Now my problem:
    I would have expected, that if I enable / disable the actionmap in one script, it would globally enable / disable it for all scripts listening to its actions. But this does not work... The "TestOnJumpButton" script only works if I enable the actionmap in this script. Also, if I disable the Player Script (and therefore the actionmap with it) during runtime, the "TestOnInput()" function is still being called whenever the Jump action is activated.

    Am I overlooking something? Is this intented behaviour?
     
  2. This is your clue. You're creating a new instance for this script. If you want a global one, make ONE global instance and reference it everywhere. This is basic C# behavior and programming.
     
  3. schwmm

    schwmm

    Joined:
    Oct 20, 2020
    Posts:
    62
    Thank you very much for your reply!
    I kind of assumed it would be something like this, however I'm not expirienced enough (yet) with Unity or coding to have pinpointed it - so thanks for pointing it out!

    To the solution:
    I guess generally something like this could be addressed by making a singleton out of the class and attaching it to an empty gameObject (if it requires MonoBehaviour).
    However - as I have the auto-created C# class of the InputSystem, including a namespace, is there a way of skipping this step? Does it include already a global static instance of my Controls() that I could access directly from different scripts somehow?