Camera.main is null when performing raycast
Asked Answered
L

4

8

Code that generates an error:

void Update()
{
    if (Input.touchCount > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
        if (hit && hit.collider != null && hit.collider.name == "leftTapArea")
        {
            hit.transform.name = "Hit";
        }
    }
}

It says that something is wrong with this string:

RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);

Error:

NullReferenceException: Object reference not set to an instance of an object leftScript.Update () (at Assets/leftScript.cs:16)

Lorileelorilyn answered 15/1, 2017 at 10:13 Comment(0)
M
19

The only thing that can return null in your code is Camera.main.ScreenToWorldPoint. It means that Camera.main is null. For Camera.main to be initialized, the camera must have the MainCamera tag.

Select the Camera GameObject then change the tag to MainCamera.

enter image description here

If you don't want your camera to be in the MainCamera tag, you can also find wit directly with GameObject.Find then get the Camera component from it.

Camera cam;

void Start()
{
    cam = GameObject.Find("NameOfCameraGameObject").GetComponent<Camera>();
}

void Update()
{
    if (Input.touchCount > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
        if (hit && hit.collider != null && hit.collider.name == "leftTapArea")
        {
            hit.transform.name = "Hit";
        }
    }
}
Millionaire answered 15/1, 2017 at 10:32 Comment(3)
Thanks a lot, it helped.Lorileelorilyn
I see what you meant by my duplicated question.... Let me try your solution... Thanks.Hearne
GREAT!! Thanks a lotAssortment
D
2

Make sure you have in your scene an active gameobject with the Camera component and the tag "MainCamera"

Tag
(source: unity3d.com)

Darnel answered 15/1, 2017 at 10:29 Comment(0)
G
0

In case you do not add a "Main Camera" gameobject per default to the scene and initialize it during runtime, this is how you would do it properly:

//This is needed in order for UnityEngine.Camera.main... to work
public const string CameraGameObjectName = "MainCamera";

content of the init method:

GameObject cameraGameObject = new GameObject(CameraGameObjectName);
cameraGameObject.tag = CameraGameObjectName;
UnityEngine.Camera camera = cameraGameObject.AddComponent<UnityEngine.Camera>();
Transform cameraPosition = cameraGameObject.GetComponent<Transform>();

You can access it afterwards from anywhere with:

UnityEngine.Camera.main.
Gnat answered 10/11, 2021 at 14:44 Comment(0)
E
0

I have the same error. But my error is the name of the camera script, i named it Camera with capital "C". So i could not access to Camera.main. And i changed the name of the script more importantly the name of the class from 'Camera' to 'camera'. Then there are no more errors. This is late but i suppose this might help others.

Elna answered 11/8, 2022 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.