UnityEngineCameraBindings.gen.cs throwing a NullReferenceException when using the Camera.ScreenToWorldPoint() Method.

A portion of my code is supposed to convert each touches screen pixel coordinates to a Vector2 World Point (Unity transform units). When I run the script (and touch the touchscreen I have connected), I get a NullReferenceException, yet VisualStudio never reported any problems within the script that I wrote. When I checked the Error List I got this feedback:
[72318-problem.png*_|72318]
!Here’s a paste able version of the code in the picture:

void Update()
        {
            for (int i = 0; i < Input.touches.Length; i++)
            {
                Camera camera = new Camera();
                Vector3 worldPoint3d = camera.ScreenToWorldPoint(new Vector3(Input.touches_.position.x,Input.touches*.position.y,0));*_

Vector2 worldPoint2d = new Vector2(worldPoint3d.x, worldPoint3d.y);
}
}
The first thing I noticed was that the error was reported on line 458, while the script i’m writing currently only has 55 lines. I also noticed that it was reported in a file called “UnityEngineCameraBindings.gen.cs”. The code you see in the picture does in fact use the UnityEngine.Camera.ScreenToWorldPoint() which is a Unity documented method. I may be an amateur programmer but I can deduce that the problem may not lie with my script, rather somewhere in the Camera class of the UnityEngine namespace.
I’d appreciate any suggestions on how I might be able to fix this, feedback on whether it’s just me that’s experiencing it or if its bug in the latest Unity update (5.3.5f1), or possibly any alternative way of scripting the function that the code in the picture performs.
_*

Do they appear in the hierarchy? If so it may be an issue with positioning or scaling. I can't see any problems with your code at a glance but i'm not in a position to test it.

1 Answer

1

Try changing

Camera camera = new Camera();

to

Camera camera = GetComponent<Camera>();

This will obtain the camera game object from the scene, as opposed to creating a new camera which is likely unattached to the scene.

Thank you for reminding me of the hierarchy. It was spawning, but it was spawning away from the screen.