How can i fix this error..... (Very new to C#)

Hey anyone know how to fix this error ‘NullReferenceException: Object reference not set to an instance of an object’
Here is my script, any help is appreciated:

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

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private LayerMask platformLayerMask;
    public Rigidbody2D rb;
    float moveLeft = -2000f;
    float moveRight = 2000f;
    float jumpForce = 10f;
    private BoxCollider2D boxCollider2D;
 


    void Start()
    {
        rb.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if(IsGrounded() && Input.GetKeyDown(KeyCode.W))
        {
            rb.velocity = Vector2.up * jumpForce;
        }
    }
    public bool IsGrounded ()
    {
        float extraHeightTest = 1f;
        RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, extraHeightTest, platformLayerMask);
    
    Color rayColor;
        if (raycastHit.collider != null)
        {
            rayColor = Color.green;
        }
        else {
            rayColor = Color.red;
        }
        Debug.DrawRay(boxCollider2D.bounds.center, Vector2.down * (boxCollider2D.bounds.extents.y + extraHeightTest));
        Debug.Log(raycastHit.collider);
        return raycastHit.collider != null;

    }
 

    void FixedUpdate ()
    {
        float xdirection = Input.GetAxisRaw("Horizontal");
        float ydirection = Input.GetAxisRaw("Vertical");

        if(xdirection == -1)
        {
            rb.AddForce(new Vector2(moveLeft * Time.deltaTime, 0f));
        }
        else if (xdirection == 1)
        {
            rb.AddForce(new Vector2(moveRight * Time.deltaTime, 0f));
        }
    
    }
}

Please edit your post to use code-tags . Walls of plain-text are hard to read.

We’ll need to guess which line because you didn’t post the full error which even tells you the line and column. These exceptions are nothing to do with Unity but C# and are by far the most common error new users to the language post on these forums. If you don’t know what a reference is then I would highly recommend you look that up (search for “C# reference value types”; there’s plenty of online tutorials though.

In short: References are things that refer to instances of objects such as your “boxCollider2D” one above. References default to NULL so if you don’t assign anything to it, you’ll get this when you try to do something with it. NULL means nothing, empty, nada, nil, zilch.

If I had to guess, it’s this line which makes no sense: “rb.GetComponent();” I guess you’re trying to assign a Rigidbody2D to “rb” so the error will be here. Presumably you should meant or should’ve copied “rb = GetComponent();”? It’s public so it’s not clear if you’re assing that via the inspector or not.

Certainly you’re not assigning anything to “boxCollider2D” so maybe the above line shoudl be assigning a BoxCollider2D there.

Okay thank you for the feedback! I will get rid of “rb.GetComponent();” as if was apart of an earlier version of the player movement but it was scraped and serves no purpose now.

Andddd now it works! changed the boxCollider@d from private to public and then assigned it in the inspector. Thanks!

1 Like

I went ahead and added code-tags for you to demonstrate their usage.