Search Unity

Change PlayerInput actions at runtime

Discussion in 'Input System' started by Heimlink, Sep 15, 2019.

  1. Heimlink

    Heimlink

    Joined:
    Feb 17, 2015
    Posts:
    29
    I'm trying to modify my PlayerInput actions at runtime.

    If I set the actions and map directly, I get some orphaned actions and the users don't seem to have the correct actions configured. As you can see below the "Keyboard" user's Pause action is mapped to the XBox Gamepad start button.
    Code (CSharp):
    1. playerInput.actions = actions;
    2. playerInput.SwitchCurrentActionMap(actionMap); // "Pause Map"
    Screen Shot 2019-09-15 at 9.51.07 am.png

    However if I disable the PlayerInput before setting the actions, and then re-enable it, the users are configured as I expect, without any orphaned actions.
    Code (CSharp):
    1. playerInput.enabled = false;
    2. playerInput.actions = actions;
    3. playerInput.enabled = true;
    4. playerInput.SwitchCurrentActionMap(actionMap); // "Pause Map"
    Screen Shot 2019-09-15 at 9.58.51 am.png

    I expect this is because toggling disabled/enable re-configures the PlayerInput, where as setting the action doesn't.

    Is this the best way to go about setting the PlayerInput actions manually?

    Thanks,