How can ı solve null reference exception error

NullReferenceException: Object reference not set to an instance of an object
Player.PlayerController.ApplyMovement () (at Assets/Scripts/Player/PlayerController.cs:124)
Player.PlayerController.FixedUpdate () (at Assets/Scripts/Player/PlayerController.cs:102)

This is the mistake ı am getting How can ı fix this problem

using System;
using ProInput.Scripts;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Player {

    public class PlayerController : MonoBehaviour {



        Animator animator;
        public bool Run;
        public bool RunningJump;
        public AudioClip _soundd;    // this lets you drag in an audio file in the inspector
        public AudioSource audioo;

        void Awake()
        {
            audioo = audioo; //oadds an AudioSource to the game object this script is attached to
            audioo.playOnAwake = false;
            audioo.clip = _soundd;
            audioo.Stop();
            Run = false;
            animator = GetComponent<Animator>();
            GetComponent<Rigidbody>();  //Set the speed of the GameObject

        }


        [Header("Movement")]
        public float movementSpeed;
        public float Accelaration;
        public float airMovementSpeed;
        public float maxSpeed;


        [Header("Friction")]
        public float friction;
        public float airFriction;

        [Header("Rotation")]
        public float rotationSensitivity;
        public float rotationBounds;

        [Header("Gravity")]
        public float extraGravity;

        [Header("Ground Detection")]
        public LayerMask whatIsGround;
        public float checkYOffset;
        public float checkRadius;
        public float groundTimer;

        [Header("Jumping")]
        public float jumpForce;
        public float jumpCooldown;

        [Header("Data")]
        public Camera playerCamera;
        public Transform playerCameraHolder;
        public Rigidbody playerRigidBody;

        private InputObject forwardsInput;
        private InputObject backwardsInput;
        private InputObject leftInput;
        private InputObject rightInput;
        private InputObject _jumpInput;
        private InputObject _runInput;

        private float _xRotation;
        private float _yRotation;
        private float _grounded;
        private bool _realGrounded;
        private float _jumpCooldown;


        private void Start() {


          forwardsInput = new InputObject("Forwards", Key.W);
            backwardsInput = new InputObject("Backwards", Key.S);


            _runInput = new InputObject("run", Key.LeftShift  );

            leftInput = new InputObject("Left", Key.A);
            rightInput = new InputObject("Right", Key.D);
            _jumpInput = new InputObject("Jump", Key.Space);
        }

        private void FixedUpdate() {

            GroundCheck();


ApplyMovement();


        }

        private  void Update() {


            Rotation();
  ApplyGravity();
  ApplyFriction();

            RollJumping();
        }


        private void ApplyMovement() {


          if (_realGrounded==false)

          {
              var axis = new Vector2(
             (leftInput.IsPressed ? -1 : 0) + (rightInput.IsPressed ? 1 : 0),



             (backwardsInput.IsPressed ? 0 : 0) + (forwardsInput.IsPressed ? 0 :0)
         ).normalized;

              var speed = _realGrounded ? movementSpeed : airMovementSpeed;

              var vertical = axis.y * speed * Time.fixedDeltaTime * transform.forward;


              var horizontal = axis.x * speed * Time.fixedDeltaTime * transform.right;

              if (CanApplyForce(vertical, axis))
                  playerRigidBody.velocity += vertical;

              if (CanApplyForce(horizontal, axis))
                  playerRigidBody.velocity += horizontal;

          }
      }

Seems like you’ve edited the script since the errors, as the line numbers in the error don’t line up.

Double check that you’ve assigned Player Rigid Body in the inspector window. That might be the null reference. But hard to tell since line numbers don’t match up.

actually there more parts in code like gravity , friction and rigidbody is working perfectly in them. Only problem is movement. I cant understand what is wrong with “apply movement”.

Did you write this code, or did you copy it? Regarding the error, simply back up to the point where it was working, then share with us the exact code that you added that started to generate this error. I trust you didn’t type this entire code in, before testing. And please don’t “leave out” code if you are posting here, please post the exact code so the errors coincide with the line numbers.

(leftInput.IsPressed ? -1 : 0) + (rightInput.IsPressed ? 1 : 0),
             (backwardsInput.IsPressed ? 0 : 0) + (forwardsInput.IsPressed ? 0 :0)
         ).normalized;
1 Like

It actually doesn’t matter what you are doing. Why?

Because the answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

If you actually ARE just blindly hammering code in from a tutorial or somewhere you found on the net, this will save you a LOT of time and frustration:

How to do tutorials properly:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!