0
\$\begingroup\$

I'm following a tutorial showing how to use the new input management system in Unity, but I'm stuck at this part:

I've created the InputSystem and a action map for my "player" object called PlayerInputActions, in it I defined a action called "Fire" which is bound to left mouse button and left shift (for testing). I've also added this action map to my "player" object as well as my C# script called PlayerShooting, it looks like this:

[SerializeField]
private PlayerInputActions playerInput;

private void OnEnable()
{
    Debug.Log("Enabling");
    playerInput.Player.Fire.performed += OnFire;
    playerInput.Player.Fire.Enable();
}

private void OnDisable()
{
    playerInput.Player.Fire.performed += OnFire;
    playerInput.Player.Fire.Disable();
}

private void OnFire(InputAction.CallbackContext context)
{
    Debug.Log("Fire");
}

Now the next step in the tutorial is running the game and looking at the output console to see if it worked or not, and of course for the guy doing the tutorial it worked, but I (expectedly) get an error in the console:

NullReferenceException: Object reference not set to an instance of an object PlayerMovement.OnEnable () (at Assets/PlayerShooting.cs:12)
NullReferenceException: Object reference not set to an instance of an object PlayerMovement.OnDisable () (at Assets/PlayerShooting.cs:18)
Assets\PlayerShooting.cs(7,32): warning CS0649: Field 'PlayerMovement.playerInput' is never assigned to, and will always have its default value null

So I went back to editor to see if I can set the playerInput field of the PlayerShooting script on my "player" GameObject, but for some reason I can't see the field there.

So, how do I set the playerInput field on my "player" GameObject?

I reworked it a little and here is the new PlayerInputActions:

public class @PlayerControls : IInputActionCollection, IDisposable
{
    public InputActionAsset asset { get; }
    public @PlayerControls()
    {
        asset = InputActionAsset.FromJson(@"{
    ""name"": ""PlayerControls"",
    ""maps"": [
        {
            ""name"": ""Player"",
            ""id"": ""448d37d3-a4af-479e-8c8f-ad8fc97cb397"",
            ""actions"": [
                {
                    ""name"": ""Fire"",
                    ""type"": ""Button"",
                    ""id"": ""28cd5757-df8b-4428-85a0-c004ea56d718"",
                    ""expectedControlType"": """",
                    ""processors"": """",
                    ""interactions"": """"
                },
                {
                    ""name"": ""Jump"",
                    ""type"": ""Button"",
                    ""id"": ""ea20c10e-9876-44bf-9067-618e6fcdf724"",
                    ""expectedControlType"": """",
                    ""processors"": """",
                    ""interactions"": ""Press""
                },
                {
                    ""name"": ""Movement"",
                    ""type"": ""Button"",
                    ""id"": ""a07f2f71-ad1c-4a3b-837c-7fb5f7a3022e"",
                    ""expectedControlType"": """",
                    ""processors"": """",
                    ""interactions"": """"
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""dec75062-6672-48d5-b1f5-ddec4799a18a"",
                    ""path"": ""<Mouse>/leftButton"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Fire"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": """",
                    ""id"": ""bfb9d278-bc38-4552-9d4f-7946469ddb3c"",
                    ""path"": ""<Keyboard>/space"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Jump"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""WASD"",
                    ""id"": ""0d93449f-0f8f-4d2c-baa3-89f3a45e34f8"",
                    ""path"": ""2DVector"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": true,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""up"",
                    ""id"": ""2fe6e7ba-01af-4e5c-b061-8ed2f30331ba"",
                    ""path"": ""<Keyboard>/w"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""down"",
                    ""id"": ""3e6c2b0b-f283-4e00-a7b6-f51b44ec6c76"",
                    ""path"": ""<Keyboard>/s"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""left"",
                    ""id"": ""42a6d504-4650-4c5f-92ef-b58c2d9450d7"",
                    ""path"": ""<Keyboard>/a"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""right"",
                    ""id"": ""bf4596ba-08bb-4809-92c0-90dd781463ad"",
                    ""path"": ""<Keyboard>/d"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""Arrows"",
                    ""id"": ""a972e8d8-1528-4f42-9dc2-ec84ca3636f3"",
                    ""path"": ""2DVector"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": true,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""up"",
                    ""id"": ""b3df1bd6-964e-400f-a847-1ecca2a2e995"",
                    ""path"": ""<Keyboard>/upArrow"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""down"",
                    ""id"": ""c77c1ea6-b730-4600-bb1c-b156750e607d"",
                    ""path"": ""<Keyboard>/downArrow"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""left"",
                    ""id"": ""c8ddea3a-a62c-42cc-a5e8-b8bef3737f20"",
                    ""path"": ""<Keyboard>/leftArrow"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""right"",
                    ""id"": ""555665d5-d351-4bef-8eba-3335074297b9"",
                    ""path"": ""<Keyboard>/rightArrow"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Movement"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                }
            ]
        }
    ],
    ""controlSchemes"": []
}");
        // Player
        m_Player = asset.FindActionMap("Player", throwIfNotFound: true);
        m_Player_Fire = m_Player.FindAction("Fire", throwIfNotFound: true);
        m_Player_Jump = m_Player.FindAction("Jump", throwIfNotFound: true);
        m_Player_Movement = m_Player.FindAction("Movement", throwIfNotFound: true);
    }

    public void Dispose()
    {
        UnityEngine.Object.Destroy(asset);
    }

    public InputBinding? bindingMask
    {
        get => asset.bindingMask;
        set => asset.bindingMask = value;
    }

    public ReadOnlyArray<InputDevice>? devices
    {
        get => asset.devices;
        set => asset.devices = value;
    }

    public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;

    public bool Contains(InputAction action)
    {
        return asset.Contains(action);
    }

    public IEnumerator<InputAction> GetEnumerator()
    {
        return asset.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Enable()
    {
        asset.Enable();
    }

    public void Disable()
    {
        asset.Disable();
    }

    // Player
    private readonly InputActionMap m_Player;
    private IPlayerActions m_PlayerActionsCallbackInterface;
    private readonly InputAction m_Player_Fire;
    private readonly InputAction m_Player_Jump;
    private readonly InputAction m_Player_Movement;
    public struct PlayerActions
    {
        private @PlayerControls m_Wrapper;
        public PlayerActions(@PlayerControls wrapper) { m_Wrapper = wrapper; }
        public InputAction @Fire => m_Wrapper.m_Player_Fire;
        public InputAction @Jump => m_Wrapper.m_Player_Jump;
        public InputAction @Movement => m_Wrapper.m_Player_Movement;
        public InputActionMap Get() { return m_Wrapper.m_Player; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
        public void SetCallbacks(IPlayerActions instance)
        {
            if (m_Wrapper.m_PlayerActionsCallbackInterface != null)
            {
                @Fire.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnFire;
                @Fire.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnFire;
                @Fire.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnFire;
                @Jump.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
                @Jump.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
                @Jump.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
                @Movement.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
                @Movement.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
                @Movement.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
            }
            m_Wrapper.m_PlayerActionsCallbackInterface = instance;
            if (instance != null)
            {
                @Fire.started += instance.OnFire;
                @Fire.performed += instance.OnFire;
                @Fire.canceled += instance.OnFire;
                @Jump.started += instance.OnJump;
                @Jump.performed += instance.OnJump;
                @Jump.canceled += instance.OnJump;
                @Movement.started += instance.OnMovement;
                @Movement.performed += instance.OnMovement;
                @Movement.canceled += instance.OnMovement;
            }
        }
    }
    public PlayerActions @Player => new PlayerActions(this);
    public interface IPlayerActions
    {
        void OnFire(InputAction.CallbackContext context);
        void OnJump(InputAction.CallbackContext context);
        void OnMovement(InputAction.CallbackContext context);
    }
}

```
\$\endgroup\$
7
  • \$\begingroup\$ Can you include a link to the tutorial you're following? \$\endgroup\$
    – DMGregory
    Commented Mar 15, 2020 at 15:14
  • \$\begingroup\$ Try removing [serializeField] flag and changing variable to public, does it work that way? \$\endgroup\$
    – Nick
    Commented Mar 15, 2020 at 15:14
  • \$\begingroup\$ @Nick already tried that, it doesn't work. Turns out you're supposed to instantiate the input actions manually in something like Awake or OnEnable \$\endgroup\$
    – MindSwipe
    Commented Mar 15, 2020 at 15:23
  • \$\begingroup\$ @DMGregory I was following this tutorial, but it turns out it's outdated and you're now supposed to instantiate the input actions manually in Awake or OnEnable or wherever \$\endgroup\$
    – MindSwipe
    Commented Mar 15, 2020 at 15:24
  • 1
    \$\begingroup\$ Hmm, the name PlayerInputActions doesn't match the class name @PlayerControls or its struct PlayerActuons, and neither one is marked serializable. What are we missing here? \$\endgroup\$
    – DMGregory
    Commented Mar 15, 2020 at 15:51

1 Answer 1

1
\$\begingroup\$

With the newer versions you need to instantiate the controls like so

    private void Awake() {
        playerInput = new PlayerInputActions();
    }
    private void OnEnable() {
        playerInput.Enable();
    }
    private void OnDisable() {
        playerInput.Disable();
    }
    void Start()
    {
        playerInput.Player.Fire.performed += _ => OnFire();
    }

I have a tutorial here for more information: Link

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .