'Rigidbody2D' could not be found, but I know that I typed it correctly

Hello, as the title says, I’ve created a rigidbody variable but it says it can’t find it and I can’t for the life of me figure out why! Here is all of my code below.

public bool isGrounded;

public float speed;

public float drag;

public Rigidbody2D body;

public BoxCollider2D groundCheck;

public LayerMask groundMask;

// Update is called once per frame
void Update() //based off of frame rate. good for inputs.
{
    //set the input access to horizontal movement, then multiply the direction by speed to move forward
    float xInput = Input.GetAxis("Horizontal");
    float yInput = Input.GetAxis("Vertical");

    if (Mathf.Abs(xInput) > 0)
    {
        body.velocity = new Vector2(xInput * speed, body.velocity.y);
    }
    
    if (Mathf.Abs(yInput) > 0)
    {
        body.velocity = new Vector2(body.velocity.x, yInput * speed);
    }
}

void FixedUpdate() //based off of tick update. good for physics simulation.
{
    CheckGround();

    if (isGrounded)
    {
        body.velocity *= drag;
    }
    
}

void CheckGround()
{
    isGrounded = Physics2D.OverlapAreaAll(groundCheck.bounds.min, groundCheck.bounds.max, groundMask).Length > 0;
}

You need to use GetComponent to have the body variable reference it.

In an Awake method add body = GetComponent<Rigidbody2D>(); to get the RigidBody component you have added to the game object that has this script.

As an alternative you can also check the TryGetComponent.

This is not a useful summary of the error:

If it can’t find the identifier Rigidbody2D, that means either you didn’t include the right using statement for it, you didn’t type it correctly, or some other syntax error is causing you a compiler error.

If it is null, that’s just a null reference, in which case drag it in. That’s what (I think) Meredoth thinks is wrong.

Either way, The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

AND of course, if it’s just a nullref…

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

1 Like