Bug with Camera class in Unity?

I copied code from the documentation into a new C# class named example:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    public RaycastHit hit;
    void Example() {
        if (Physics.Raycast(ray, out hit, 100))
            Debug.DrawLine(ray.origin, hit.point);
        
    }
}

And guess what it spits out:

How is this possible I know that Camera.main has always worked, so my question is could this be a bug?
Because if it is I can’t continue working.

The only thing I imagine it could be is that I still have not updated to the latest version.

Help is greatly appreciated!

I made a stupid mistake by having a class for my camera in there called Camera. Stupid :stuck_out_tongue:

1 Like

i have exact same problem i dont understand what you can you describe me

i want access my camera form Camera.main but main is not showup

Create a new thread and post your code with some info of your basic scene structure setup.

Be sure you haven’t in your project a class named “Camera”, which would override the UnityEngine.Camera class, as SimonAlkemade2 had.

@SimonAlkemade2: However your code is logically wrong, cause you’re creating a Ray from the Input.mousePosition when your component is first initialized. You may want to move the assignment of ‘ray’ into the function Example to be able to make a ray at runtime when you call the function (I’d move both variable declarations inside the function since I suppose you don’t need them from outside the function).

2 Likes

Thank you you are my savior

I looked 30min for the answer and then i realize its just my script’s name :stuck_out_tongue: