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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding new bindings to existing actions at runtime

Discussion in 'Input System' started by nickleplated, Dec 10, 2019.

  1. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    I am having an issue with adding new bindings at runtime.

    I have several existing actions in an action map and I need to allow the user to both rebind them and add completely new bindings.

    I am using code similar to this:
    Code (CSharp):
    1. myAction.AddBinding(newBindingPath);
    After running this, I am able to see the new binding by accessing the bindings of the action:

    Code (CSharp):
    1. myAction.bindings[0].effectivePath;
    However, it does not trigger and it does not show up in the Input Debugger window (under actions).

    I can add multiple new bindings like this and they all react the same.

    If I then remap a binding (it can be a previously existing one or one of the new ones):

    Code (CSharp):
    1. myAction.ApplyBindingOverride(bindingOverride);
    I get an error in the console:

    Binding count in old and new state must be the same
    UnityEngine.InputSystem.InputActionMap:Enable()​

    Then all the new bindings show up in the debugger and they start working.

    I have tried disabling the action map before adding the new bindings, but it doesn't help.
    Is there some method call that I need to make after adding a new binding in order to get it to refresh itself?
     
    ViRaKidney and Max-K127 like this.
  2. Sprakle

    Sprakle

    Joined:
    Jun 8, 2013
    Posts:
    31
    It would be really nice if we could do this! For now I've just had to add blank palceholder bindings as a workaround.