Problem with code after switching to new input system

Hi, i’ve just recently tried to swap my game from the old input system and I keep getting NullReferenceException: Object reference not set to an instance of an object error.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Movement : MonoBehaviour
{

    public float walkSpeed = 8f;
    public float sprintSpeed = 14f;
    public float maxVelocityChange = 10f;
    [Space]
    public float airControl = 1.5f;
    [Space]
    public float jumpHeight = 5f;

    public Controlls controls;

    private Vector2 input;

    private Rigidbody rb;

    private bool sprinting;
    private bool jumping;

    private bool grounded = false;

   
   

   

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();

    

    }

    // Update is called once per frame
    void Update()
    {
        input = controls.Gameplay.Walk.ReadValue<Vector2>();
        input.Normalize();
       
        sprinting = controls.Gameplay.Sprint.triggered;
        jumping = controls.Gameplay.Jump.triggered;

also, I have 3 actions in a .inputactions file (with respective code, called Controlls.cs):

  • Walk
    -Jump
    -Sprint

There is more beyond this, but I stopped at the inputs. Unity says the problem was in line 41.

I’ve spent lots of time researching this online and I have watched lot’s of tutorials but I genuinely can’t get it to work.

Thanks.

You need to new() (aka, instantiate) your controls field in Awake/Start. Otherwise it’s just a null reference type.

How would I do that?

Assuming the misspelt Controlls is your C# generated class, literally just:

private void Awake()
{
    controls = new Controlls();
}

Thanks! It works now.

1 Like