error with movement in unity following a tutorial

0
i’m trying to make a game on unity using C# for a games dev course. the course is:

and at 51:55 is where i am experiencing the error. the error is
NullReferenceException: Object reference not set to an instance of an object player.FixedUpdate () (at Assets/scripts/player.cs:32)
i have checked and it seems to be letter by letter (including caps) perfect, unless i’m jsut stupid. it may be because of outdated code, something in unity or me just copying something down wrong. anyhow, here’s the code:

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

[RequireComponent(typeof(BoxCollider2D))]
public class player : MonoBehaviour
{

    private BoxCollider2D boxCollider;
    private RaycastHit2D hit;
    private Vector3 moveDelta;

    private void start(){
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void FixedUpdate()
    {
       float x = Input.GetAxisRaw("Horizontal");
       float y = Input.GetAxisRaw("Vertical");

        // reset move delta
       moveDelta = new Vector3(x, y, 0);

        // swap sprite direction
        if(moveDelta.x > 0)
            transform.localScale = Vector3.one;
       
        else if (moveDelta.x < 0)
            transform.localScale = new Vector3(-1,1,1);

            hit =  Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("actor", "blocking"));
            if (hit.collider == null)
            {
             transform.Translate(0, moveDelta.y * Time.deltaTime, 0);   
            }

            hit =  Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("actor", "blocking"));
            if (hit.collider == null)
            {
             transform.Translate( moveDelta.x * Time.deltaTime, 0, 0);   
            }
   
    }
  
   

}

Welcome! This is such a common error we have a pinned forum post for it.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

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

LayerMask.GetMask(“actor”, “blocking”)

do you have that layer set?

Well, for claiming you checked and everything is letter per letter INCLUDING caps, you did not look close enough. I’d suggest checking again.

And look at the link Kurt posted, because you could easily solve this after you solve step 1.

Oh wow, good spot Brath… didn’t even see that. But as you note, three steps will always find it.

OP, you did NOT get all the capitalization correct. Specifically, one of your functions is never going to be called by Unity, even though you think it probably will be.

You really want to go through the three step process though because like I said, if you’re not getting this error every hour or two, you’re not really coding. :slight_smile: It’s that common. I just had one half an hour ago so I’m just about due for another one.