NullReferenceException: Object reference not set to an instance of an object

Main file: PlayerWalk.cs


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerWalk : MonoBehaviour

{

    [SerializeField]
    private float moveSpeed = 6f, jumpForce = 10f;

    private Rigidbody2D myBody;

    private Vector3 tempPos;

    private PlayerAnimation playerAnim;

    private void Awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        playerAnim = GetComponent<PlayerAnimation>();
    }

    private void Update()
    {
        //HandleMovementWithTransform();

        HandlePlayerAnimations();
    }

    private void FixedUpdate()
    {
        HandleMovementWithRigidBody();
    }

    void HandleMovementWithTransform()
    {

        tempPos = transform.position;
        
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            tempPos.x -= moveSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            tempPos.x += moveSpeed * Time.deltaTime;
        }

        transform.position = tempPos;

    }

    void HandleMovementWithRigidBody()
    {

        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {

            myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);

            //myBody.AddForce(new Vector2(-moveSpeed, 0f), ForceMode2D.Impulse);
            
        }
        else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {

            myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);

            //myBody.AddForce(new Vector2(moveSpeed, 0f), ForceMode2D.Impulse);
            
        }
        else
            myBody.velocity = new Vector2(0f, myBody.velocity.y);
        

    }

    void HandlePlayerAnimations()
    {
        playerAnim.Play_WalkAnimation((int)Mathf.Abs(myBody.velocity.x));
    }
}

Animation file: PlayerAnimation.cs


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerAnimation : MonoBehaviour
{

    private Animator anim;

    private SpriteRenderer spriteRenderer;

    private void Awake()
    {
        anim = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    public void Play_WalkAnimation(int walk)
    {
        anim.SetInteger(TagManager.WALK_ANIMATION_PARAMETER, walk);
    }
}

I am getting the error:
NullReferenceException: Object reference not set to an instance of an object
PlayerWalk.HandlePlayerAnimations () (at Assets/Scripts/Player Scripts/PlayerWalk.cs:81)
PlayerWalk.Update () (at Assets/Scripts/Player Scripts/PlayerWalk.cs:27)

I have created another script and calling that inside the awake method and still getting the error. Why it’s so?

Please help me out with the error.

Thanks

There isn’t enough here to give a definitive answer, but my first recommendation is to check to see that the GameObject that the PlayerWalk script is attached to does indeed have both Rigidbody2D and PlayerAnimation components on it. I also suggest adding the following to the end of your Awake() method.

Debug.Assert(myBody != null);
Debug.Assert(playerAnim != null);

That is a good practice which will help you narrow down problems if a component is unintentionally removed.