I get the null reference exception error and I cant figure it out.

Im new and following Brackeys tutorial. Im on episode three and am just starting to implement movement, whats wrong here?

In the tutorial you’re following the Rigidbody component is assigned as follows…

  • Select the ‘Player’ object in the hierarchy
  • Locate the ‘Player Movement’ script in the inspector
  • Select the ‘Player’ object in the ‘Rb’ slot

Alternatively, you could assign the Rigidbody reference in the script…

using UnityEngine;

public class PlayerMovement : MonoBehaviour {
    private Rigidbody rb;
    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // --
    void Awake() {
        rb = GetComponent<Rigidbody>();
    }
    // --
    void FixedUpdate() {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        if (Input.GetKey("d")) {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("a")) {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (rb.position.y < -1f) {
            FindObjectOfType<GameManager>().EndGame();
        }
    }
}