I am new to Unity, and I have a tiny bit of code which I actually took directly from a tutorial for Unity. The tutorial can be found here, at about 12:46
https://www.youtube.com/watch?v=7C7WWxUxPZE
The script is properly attached to the game object, and the game object has a Rigidbody Component.
The tutorial is a couple years old, but I looked things up in the API and everything appears to be the same as far as this particular bit of code is concerned.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
void Start()
{
rb.GetComponent <Rigidbody> ();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement);
}
}
I get NRE at two locations:
rb.GetComponent <Rigidbody> ();
rb.AddForce (movement);