NullReferenceException

在Unity3D中获取鼠标坐标并转化为世界坐标时,使用:

using UnityEngine;
using System.Collections;

public class MeshScript : MonoBehaviour {
    
    Vector3 mouseScreenPos, mouseWorldPos;
    // Update is called once per frame
    void Update () 
    {    
        // for mouse
        mouseScreenPos = Input.mousePosition;
        print(mouseScreenPos);
        mouseScreenPos.z = 1.0f;
        mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos);
        print(mouseWorldPos);
    }
}

出现错误:NullReferenceException
UnityEngine.Camera.ScreenToWorldPoint (Vector3 position) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:267)
MeshScript.Update () (at Assets/MeshScript.cs:38)

原因:没有相机被设为主相机。

解决方法:将其中一个相机的Tag由Untagged改为MainCamera,问题解决。

posted on 2013-01-22 15:38  没有什么能够阻挡  阅读(1448)  评论(0编辑  收藏  举报

导航