Object Reference not set to instance of object?

By the way i’m pretty much completely new to Unity and C# and this is my first project. for someone reason after getting help with the characterMovement and userInput scripts i’m getting an error titled ’
NullReferenceException: Object reference not set to an instance of an object
UserInput.FixedUpdate () (at Assets/Scripts/Scripts/UserInput.cs:71)’

I literally have no idea what this means and was hoping someone here could help me figure it out. This is the script it’s referancing if it help sat all


using UnityEngine;
using System.Collections;

public class UserInput : MonoBehaviour {

    public bool walkByDefault = false;

    private CharacterMovement charMove;
    private Transform cam;
    private Vector3 camForward;
    private Vector3 move;
  
    void start()
    {
        if (Camera.main != null)
        {
            cam = Camera.main.transform;
        }

        charMove = GetComponent<CharacterMovement>();
    }

    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        if (cam != null)
        {
            camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
            move = vertical * camForward + horizontal * cam.right;
        }
        else
        {
            move = vertical * Vector3.forward + horizontal * Vector3.right;
        }

        if (move.magnitude > 1)
            move.Normalize();

        bool walkToogle = Input.GetKey(KeyCode.LeftShift);

        float walkMultiplier = 1;

        if (walkByDefault)
        {
            if (walkToogle)
            {
                walkMultiplier = 1;
            }
            else
            {
                walkMultiplier = 0.5f;
            }
        }
        else
        {
            if (walkToogle)
            {
                walkMultiplier = 0.5f;
            }
            else
            {
                walkMultiplier = 1;
            }
        }

        move *= walkMultiplier;

        charMove.Move(move);
    }
}

Could you please use code tags, it will make the code much easier to read for people.
You should check

charMove = GetComponent<CharacterMovement>();

This will be null if no CharacterMovement script is attached to the GameObject

Cheers. Sorry I didn’t realize that was a thing.

1 Like