Unable to get RigidBody2D component.

Hi all. I just installed Unity 2019.2 and created a sprite and attached the RigidBody2D component to it. I attached a very simple script to the sprite that is just declaring a private 2D object variable and I get the “All comiler errors have to be fixed…” thing. Any idea what’s wrong?:

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

public class Player : MonoBehaviour
{

    private RigidBody2D rb;

    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {

    }
}

And the B in Rigidbody has to be lower case!! … :frowning:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
   
    public class Player : MonoBehaviour
    {
   
        private Rigidbody2D rb;
   
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();     
        }
   
        // Update is called once per frame
        void Update()
        {
   
        }
    }
1 Like

If for whatever reason what @MelvMay say’s doesn’t work (which it should) you can try this:

[SerializeField]
private Rigidbody2D rb;

// And you can now drag and drop your player in the field
// inside of the inspector. You will not need to GetComponent
// using this approach.