Getting an error following the video

Greetings fellow Unity types…

I am using the CameraRayCaster.cs down loaded from the course, when I run it with the Cursor.cs that Ben makes in the video, I get the following errors:

NullReferenceException: Object reference not set to an instance of an object
CameraRaycaster.RaycastForLayer (Layer layer) (at Assets/Scripts/CameraRaycaster.cs:52)
CameraRaycaster.Update () (at Assets/Scripts/CameraRaycaster.cs:35)

This appears to be the offending section:

RaycastHit? RaycastForLayer(Layer layer)
    {
        int layerMask = 1 << (int)layer; // See Unity docs for mask formation
        Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit; // used as an out parameter
        bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
        if (hasHit)
        {
            return hit;
        }
        return null;
    }

Any thoughts/resolutions?

Many thanks in advance.

Disregard guys.

I’m getting the same thing. What did you do to solve it?

Usually good practice to say how you solved it since others (like myself) may have a similar issue.

Okay, probably the same issue for Richard. I placed the cursor.cs as a component of the Main Camera, not the Camera Arm.

Hey, I’m still haveng this issues :(((( using the latest Versions of UNITY and done absolutely everything exactly as in tutorial.

Unbenannt

It’s me again. This is why I really love the gamedev.tv courses from Ben and his Team. It forced me to think about this Problem for three hours and after I wrote my problem here, I read 40 times the error message and this pointed me to the viewCamera which is Camera.main and for some reason none of my CAM objects in unity was set to “Main Camera”!!! Usually at every new project start, the default scene has a camera set to main.

Hope this helps if anybody gets stuck at such an error. Always try to understand what the error message says literally

Kind regards

I had the same problem. To be more clear, set the TAG for CameraArm and Main Camera to MainCamera

Privacy & Terms