Issue with CamRay [Solved]

So I am trying use the Survival Shooter, as base to work on my program but I am running into a issue NullReferenceException: Object reference not to an instance of an object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cyclop_Player_Controller : MonoBehaviour {
    public float speed = 6f;
    Vector3 movement;
    Animator anim;
    Rigidbody rb;
    int groundMask;
    float camRayLength = 100f;

  void Awake()
    {
        groundMask = LayerMask.GetMask("Ground");
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("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;

        rb.MovePosition(transform.position + movement);
    }
    void Turning()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit groundHit;

        if(Physics.Raycast(camRay, out groundHit, camRayLength, groundMask))
        {
                Vector3 playerToMouse = groundHit.point - transform.position;
                playerToMouse.y = 0f;
            Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
            rb.MoveRotation(newRotation);
        }
    }
    void Animating(float h, float v)
    {
        bool walking = h != 0f || v != 0f;
        anim.SetBool("IsWalking", walking);
    }
}

It might be due how i have the scene set up but I wouldn’t be sure how I be able to tell. I appropriate the assistance cause i been using unity for 1 month so still bit new to it.

Please paste the full error message. It includes vital troubleshooting information like line numbers and context.

Sorry about late reply, What shows up is:
NullReferenceException: Object reference not set to an instance of an object Cyclop_Player_Controller.Turning()(at Assets/Cyclop_Player_Controller.cs:38)
then far right side all that shows 1031

So assuming that your line 38 and line 38 as pasted here are the same, that means that something on that line is null when you try and “dig into” it (e.g. you try to do foo.bar, but foo is null). The only possibility on that line is Camera.main being null, which will happen if you don’t have a Camera in your scene that has been tagged MainCamera. So check for that, first.

(the 1031 to the right is the number of times that error has occurred, and it’s “collapsed” them into one instance. A big number like that mostly means it has happened every frame.)

1 Like

Yes that seems to have been the problem the camera that came with the pre-set environment wasn’t set taged as the MainCamera. I thank you for your help StarManta.