空引用异常Unity 2d



我在Unity 2d 中遇到问题

NullReferenceException:对象引用未设置为对象的实例RightOne.OnMouseDown(((位于Assets/Scripts/RightOne.cs:13(UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32(

下面是脚本:

using UnityEngine;
using System.Collections;
public class RightOne : MonoBehaviour {
private GameObject mainCube;
void Start () {
mainCube = GameObject.Find ("Main Cube");
}
void OnMouseDown () {
if (GetComponent <Renderer> ().material.color == mainCube.GetComponent <Renderer> ().material.color)
mainCube.GetComponent <GameCntrl> ().next = true;
else
mainCube.GetComponent <GameCntrl> ().lose = true;
}
}

这意味着该函数中代码中的某些内容是null,例如,如果找不到对象,GameObject.Find(..)将返回null。GetComponent<T>()也可以返回null。

您需要确保找到了游戏多维数据集,并且当前游戏对象和主多维数据集实际上已经附加了渲染器组件。GameCntrl组件也是如此。

最新更新