Unity C#:Camera.main返回null? [英] Unity C# : Camera.main returns null?

查看:2398
本文介绍了Unity C#:Camera.main返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个C#代码的免费样本可以将对象移动到Unity 3D中的鼠标点击位置,如下所示:

 公共GameObject立方体; 
Vector3 targetPosition;

void Start(){

targetPosition = transform.position;
}
void Update(){

if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit点击;

if(Physics.Raycast(ray,out hit)){
targetPosition = hit.point;
cube.transform.position = targetPosition;
}
}
}

====== ========



问题是在运行时,Unity会在行生成错误:



Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);



错误消息为: NullReferenceException:对象引用未设置为对象的实例 ...



根据某人的建议,我刚刚在上面的示例代码中添加了一些调试语句,并且发现Camera.main为NULL。因此,这是Unity生成上述错误消息的主要原因。 : - )



请注意我整个游戏项目只有1个摄像头。



这是我的主摄像头的捕获图像,已经启用并由Unity自动标记为主摄像头。但是,这也无法解决问题。





< hr>

最终更新:



我刚刚发现stackoverflow用户程序员已经发布了一个很好的答案:





这是一个带有一些下拉菜单默认标签和主摄像头就是其中之一。


There is a free sample of C# code to move an object to a mouse click position in Unity 3D as shown below:

public GameObject cube;
Vector3 targetPosition;

void Start () {

    targetPosition = transform.position;
}
void Update(){

    if (Input.GetMouseButtonDown(0)){
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit)){
            targetPosition = hit.point;
            cube.transform.position = targetPosition;
        }
    }
}

==============

The problem is that at run time, Unity generates an error at the line:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

The error message is: NullReferenceException: Object reference not set to an instance of an object...

Per someone's suggestion, I have just put some debug statements in the sample code above, and found that "Camera.main" is NULL. So, that is the main reason the Unity generates the error message above. :-)

Please note that I only have 1 camera for the whole game project.

Here is the captured image of my "Main Camera" that is already enabled and tagged as "Main Camera" automatically by Unity. But, that does not fix the problem either.


FINAL UPDATE:

I have just found out that the stackoverflow user "Programmer" already posted an excellent answer at:

Raycast returns null

This answer fixed my issue when I use his code "GameObject.Find("NameOfCameraGameObject").GetComponent();"

So, I agree that my question above is kind of duplicated. However, when I asked if I should delete my question, the user "Programmer" suggested that I may keep this question open as it may still be useful for someone else to view in the future. (Again, I already post the link to the correct answer by user "Programmer" above).

My conclusion: it is strange that even though Unity automatically enables the tag "MainCamera", the code still thinks that "Camera.Main" is null. Therefore, I have to use the code written by the user "Programmer" to fix the issue.

Big thanks to the user "Programmer" and other great stackoverflow users for helping me with this question. :-)

解决方案

No you have named the gameobject main camera. Tag the gameobject as main camera (below the name, currently player).

This is a drop down menu with some default tags and Main Camera is one of them.

这篇关于Unity C#:Camera.main返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆