Survival tutorial error

I am getting the Error:
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.Turning () (at Assets/Scripts/Player/PlayerMovement.cs:55)
PlayerMovement.FixedUpdate () (at Assets/Scripts/Player/PlayerMovement.cs:34)

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;            // The speed that the player will move at.
   
    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    float camRayLength = 100f;          // The length of the ray from the camera into the scene.
   
    void Awake ()
    {
        floorMask = LayerMask.GetMask ("Floor");

        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();
    }
   
   
    void FixedUpdate ()
    {
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");

        Move (h, v);

        Turning ();

        Animating (h, v);
    }
   
    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);
        }
    }
   
    void Animating (float h, float v)
    {
        bool walking = h != 0f || v != 0f;

        anim.SetBool ("IsWalking", walking);

Missing a couple of "}"s at the end.

Sorry actually i have the “}”, i guess they got deleted while copying the code here. Also The Mistake is on 45 and 29 and not 55 and 34. (Sorry my bad) got any solution for it?

You have to set the camera in your scene to use the tag “MainCamera”, as Camera.main is just a shortcut for accessing the camera with that tag. Since it isn’t finding one, it’s returning null, and you’re calling a function on a null reference.

1 Like

Thank you so much. It worked :slight_smile: