using System.Collections.Generic;
using UnityEngine;
public class FOVCameraChange : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.LeftControl)){
Debug.Log("DOWN");
Camera.main.fieldOfView = 120;
}
else {
Debug.Log("UP");
Camera.main.fieldOfView = 60;
}
}
}我得到的错误是“对象引用没有设置为实例或对象”。
当你按下控制键的时候,我正试着把摄像机弄成两倍。
发布于 2022-08-22 19:30:07
Camera.main返回带有"MainCamera“标记的第一个(已启用)摄像机。因为它正在抛出一个NRE,你不能在你的场景中有一个带有标签的相机。要么给相机一个"MainCamera“标签,要么让一个Camera变量通过编辑器设置,就像你做任何其他变量一样:
public class FOVCameraChange : MonoBehaviour
{
[SerializeField] private Camera camera;
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.LeftControl)){
Debug.Log("DOWN");
camera.fieldOfView = 120;
}
else {
Debug.Log("UP");
camera.fieldOfView = 60;
}
}
}https://stackoverflow.com/questions/73449299
复制相似问题