Search Unity

Accessing actions through action sub-assets

Discussion in 'Input System' started by CodeBison, Apr 20, 2016.

  1. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    I'm trying to set up accessing actions through the action sub-assets as described here, but I think I might be doing something wrong. Here are the basics of what I've got:

    I've created an action map similar to the one in the example, called RPGControls, and added it to the PlayerInput component on my player. Here's the relevant section of my player controller class where I attempt to bind the actions from the action map:

    Code (csharp):
    1.         // Player Input component - needed for action bindings
    2.         PlayerInput playerInput;
    3.  
    4.         // Variables for various actions
    5.         // Player Actions (RPG Controls)
    6.         public AxisAction moveX;
    7.         public AxisAction moveY;
    8.         public AxisAction move;
    9.         ...
    10.  
    11.         void Start() {
    12.             // Find Player Input component
    13.             playerInput = GetComponent<PlayerInput>();
    14.  
    15.             // Map input actions
    16.             moveX.Bind(playerInput.handle);
    17.             moveY.Bind(playerInput.handle);
    18.             move.Bind(playerInput.handle);
    19.             ...
    The PlayerInput component is successfully assigned, but the first Bind command throws the following exception:

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.InputNew.ActionSlot`1[T].Bind (UnityEngine.InputNew.PlayerHandle player) (at Assets/Input/input-prototype/Input/ActionSlot.cs:16)
    3. CBG.PlayerController.Start () (at Assets/CBG/Scripts/PlayerController.cs:47)
    I put a breakpoint on ActionSlot.cs line 16:
    Code (csharp):
    1.             ActionMapInput map = player.GetActions(action.actionMap);
    When I examine the relevant components, I see that ActionSlot's action and control members are both null, which is obviously the immediate cause of the problem. Have I missed something obvious that would result in action and control being initialized prior to the bind call?

    Thanks!

    Edit: Forgot to mention, I'm using 5.4.b15.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Hi,

    Thanks for trying it out!

    You'll need to drag the action sub-assets from the ActionMap into the InputAction object fields in the Inspector for your script. Does that help?

    I see the doc page didn't explicitly mention that; I'll make it more clear.
     
  3. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    Ah, that makes much more sense. I was wondering why there wasn't any extra inspector fiddling involved.

    I'm really liking the new system, by the way. I'm converting an existing project, and I can already see that the clean design and layered action maps will help me out a lot.
     
    runevision likes this.
  4. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    Okay, so now I have a slightly different problem. I started setting the input actions in the inspector, and the components listed in the scriptableobject in the project view didn't match the controls I'd created in the action map. Almost half of them were called "RPGControls" in the project view. I thought this might be because I was using 5.4.0b15, so I tried in 5.3.4. I've deleted the action map and rebuilt it. Now instead of several actions shoiwing in the project view with the same name, I've got one missing.

    Here's the action map:
    upload_2016-4-23_22-25-39.png
    And here's what it looks like in project view:
    upload_2016-4-23_22-25-55.png

    Any idea what's up here?
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Not quite sure. Basically there's some bugs in 5.3 and earlier regarding how assets are shown. It should be fixed in 5.4. But for now, maybe the missing control is actually appearing as the root asset. E.g. when you select what appears to be the root asset, that might actually be the Action1 sub-asset.
     
  6. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    289
    Issue is still present in 5.4.0b15 (that's where I initially saw it). I've got a workaround so it's not critical for me. It seems to happen less if I hit apply after adding each action to the action map instead of adding them all at once, but that could be coincidence.