I have a character asset in my game with a Rigidbody component attached, as shown here
then my code as follows
public class CharController : MonoBehaviour
{
private Rigidbody rb;
private bool walkingRight = true;
void Awake()
{
rb.GetComponent<Rigidbody>();
}
// Use this for initialization
private void FixedUpdate()
{
rb.transform.position = transform.position + transform.forward * 2 * Time.deltaTime;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Switch();
}
}
private void Switch()
{
walkingRight = !walkingRight;
if (walkingRight)
{
transform.rotation = Quaternion.Euler(0, 45, 0);
}
else
{
transform.rotation = Quaternion.Euler(0, -45, 0);
}
}
}
When i start the game to test the character movement i get
NullReferenceException: Object reference not set to an instance of an object
CharController.Awake () (at Assets/Scripts/CharController.cs:13)
At line 13 is in my Awake method it is telling me that the initialization is null but i am not understanding why it is null, as i have a rigidbody attached to my character object. Looking for any help as to why this is happening .
Any help would be appreciated.
thanks
dcaldessa