NullReferenceException: Object reference not set to an instance of an object (PLEASE HELP!)

So I got this error in my code and now all my animations just refuse to work, I have been stuck at this issue for over a day now, and just got so tired of helpless fixing that I am asking for yall to help my stupidity.

Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
[Header(“Config”)]
[SerializeField] private float speed;

private PlayerActions actions;
private Rigidbody2D rb2D;
private PlayerAnimations playerAnimations;
private Vector2 moveDirection;

private void Awake()
{
    actions = new PlayerActions();
    rb2D = GetComponent<Rigidbody2D>();
    playerAnimations = GetComponent<PlayerAnimations>();
}

private void Update()
{
    ReadMovement();
}

private void FixedUpdate()
{
    Move();
}

private void Move()
{
    rb2D.MovePosition(rb2D.position + moveDirection * (speed * Time.fixedDeltaTime));
}

private void ReadMovement()
{
    moveDirection = actions.Movement.Move.ReadValue<Vector2>().normalized;
    if (moveDirection == Vector2.zero)
    {
        playerAnimations.SetMovingAnimation(false);
        return;
    }

    playerAnimations.SetMovingAnimation(true);
    playerAnimations.SetMoveAnimation(moveDirection);

}

private void OnEnable()
{
    actions.Enable();
}

private void OnDisable()
{
    actions.Disable();
}

}

This is the other animation side:

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

public class PlayerAnimations : MonoBehaviour
{
private readonly int moveX = Animator.StringToHash(“MoveX”);
private readonly int moveY = Animator.StringToHash(“MoveY”);
private readonly int moving = Animator.StringToHash(“Moving”);
private readonly int dead = Animator.StringToHash(“Dead”);

private Animator animator;

 private void Awake()
{
    animator = GetComponent<Animator>();
}

public void SetDeadAnimation()
{
    animator.SetTrigger(dead);
}

public void SetMovingAnimation(bool value)
{
    animator.SetBool(moving, value);
}

public void SetMoveAnimation(Vector2 dir)
{
    animator.SetFloat(moveX, dir.x);
    animator.SetFloat(moveY, dir.y);
}

}

Hello!

Not sure if what I am going to say is right but just checking your code I see that you are trying to access other components in Awake so it is possible they are not initialized. Try to set them in Unity’s Start() method.

So basically I would move this line:
playerAnimations = GetComponent<PlayerAnimations>();

To PlayerMovement Start() Method.

The other option I can think about is that you are missing to assign those Monobehaviours to the PlayerMovement Gameobject on the Editor.

Hopefully either of these two things will solve your issue.

In case, these things are not solving it, can you share the message error that is being displayed in the Unity Editor’s console?

If you’re gonna throw an error for us to help you with, please tell us which line it is, as the error will do exactly that - this line is the most important, in case of null reference, it makes something you refered to, is null, so, look at the line, chances are theres only a few things on it that can be null, then work back to find where it was set, then if you had try to set it, you have a choice of, it wasnt ready yet, or its not possible, or you removed it with some other code…

Hi, thank you for the reply. Sorry, my bad this is the first time doing this. Here is the error message I have received:
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.ReadMovement () (at Assets/Scripts/Player/PlayerMovement.cs:46)
PlayerMovement.Update () (at Assets/Scripts/Player/PlayerMovement.cs:28)using System.Collections;

Have you checked what I said? Because if I am not mistaken, the error is in this line, isn’t it?

playerAnimations.SetMovingAnimation(true);

I think the problem is that playerAnimations instance is null when you try to assign it during Awake call.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

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