[Help] Null reference exception

Hey guys,
first time poster here, not sure if this is the right thread…

I’ve been following the survival shooter tutorial using my own assets instead of the ones providing, however i am using the same player movement script demonstrated.
Problem is, when i run the game i get the error “Null reference exception: Object reference not set to an instance of an object” and my character refuses to move!
2218523--147702--Screenshot_1.png
Any help would be greatly appreciated.
Thanks in advance! :slight_smile:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public float speed = 6f;
    Vector3 movement;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRaylength = 100f;

    void awake()
    {
        floorMask = LayerMask.GetMask ("Floor");
        playerRigidbody = GetComponent<Rigidbody> ();
    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Move (h, v);
        Turning ();
    }


    void Move (float h, float v)
    {
        movement.Set (h, 0f, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition (transform.position + movement);
    }

    void Turning()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorHit;
        if(Physics.Raycast (camRay, out floorHit, camRaylength, floorMask))
        {
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;
            Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation (newRotation);       
        }


    }

}

Could you post the whole error? That’s only the top line of it. Below it should be some more information, mainly regarding the line the code errored out on.

Sure thing!
Hope this helps
2218543--147705--Screenshot_2.png

So it tells you the script (Assets/Scripts/PlayerMovement.cs) and the line ( :32). Using that, we can see this is the line causing the error:

        playerRigidbody.MovePosition (transform.position + movement);

So from that, we can firmly say that playerRigidbody is null, which takes us back to the awake() function where we call:

playerRigidbody = GetComponent<Rigidbody> ();

So if that’s not being set - there’s two possible issues. One: Your GameObject doesn’t have a Rigidbody attached to it or Two: awake() is never called.

Now I can say with certainty that awake() is never called because you meant Awake() with a capital A.

Hope writing out my thought process helps you debug any future issues you have.

I think it’s because your ‘Awake’ function doesn’t have a capital A.

Inside this, you’re setting the playerRigidbody, but as the function isn’t being called, it’s not being set, and thus, it has ‘no reference’ to what the playerRigidBody is.

1 Like

The problem is that the movement variable was never initialized. Try changing the declaration to:
csharp~~ ~~Vector3 movement = new Vector3();~~ ~~
If this variable is only used in the Move method it would be even better to move the declaration there. Then you could write:
csharp~~ ~~Vector3 movement = new Vector3(h, 0f, v);~~ ~~
instead of the .Set line you have now.

Edit: Oh, Vector3 is a struct. Of course… Just ignore my post. :smile:

Wow, i hoped that i’d get a few replies throughout the week, let alone being told the answer tonight!
the problem was indeed the lower case “a” in the Awake function - Rookie mistake i guess :wink:

Thanks so much for the help guys!