Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Switching Current Action Map from code

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

  1. mvinc006


    Jan 1, 2018
    I'm trying to figure out how I change the current ActionMap to another in script, i.e from Player to UI just as an example.

    I understand that the PlayerInput class has a method called
    Code (CSharp):
    1. SwitchCurrentActionMap(string mapNameOrId);

    Instead of passing a hard coded string (for obvious reasons) I'm wanting to access the ActionMaps that are already defined in the InputActionAsset I've already made (through the right click > create Input Actions menu).

    The docs say this should be possible ( though I cannot get it working.

    After trial and error for a few hours I couldn't get it to work. The closest I got was accessing the C# Class name of the InputActionAsset I'd created (in this case the default is NewInputSystem.cs) but there is no way to assign this in the inspector, and I'll get Null Reference errors because its not assigned.

    I'd then tried to access the Mappings Class directly from the PlayerInput class, but no luck there either.

    Below is some test code, it's the closest I've managed to get but I'm missing something here.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.InputSystem;
    4. using UnityEngine;
    6. public class Test : MonoBehaviour
    7. {
    9.     private NewInputSystem _ActionMappings;
    10.     public PlayerInput _playerInput;
    12.     void Start()
    13.     {
    14.         // by default we are starting with "Player" as the action map
    15.         _ActionMappings = new NewInputSystem();
    16.         Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
    18.         // I would expect this to return "UI" to the method, but its returning other jargon with it.
    19.         _playerInput.SwitchCurrentActionMap(_ActionMappings.UI.ToString());
    21.         Debug.Log("Current Action Map: " + _playerInput.currentActionMap.ToString());
    22.     }
    24. }
    Last edited: Nov 7, 2019
    tigerleapgorge likes this.
  2. Rene-Damm


    Sep 15, 2012
    There's no way currently to combine the C# classes generated for .inputactions assets and PlayerInput. An integration is planned for after 1.0.

    Using the generated class for lookups the way you have it in the code can be done. Replace "ToString()" with "name" and it should do what you intend.
    tigerleapgorge and mvinc006 like this.
  3. mvinc006


    Jan 1, 2018
    Thank you so easy I can’t believe I missed that.
    tigerleapgorge likes this.