【发布时间】:2013-10-28 13:03:50
【问题描述】:
我正在尝试将 Quest 对象添加到 Person。它对一个成功并为另一个给出了 nullreferenceexception,我在这里做错了什么? 附:播放器和请求者在 Unity 检查器中设置。
public class GameCreator : MonoBehaviour {
private Quest quest;
public Player player;
public Requestor requestor;
void Start() {
quest = createQuest();
requestor.thisPerson.SetQuest(quest); //this is the problem
player.thisPerson.SetQuest(quest);
}
}
public class Player : MonoBehaviour {
public Person thisPerson;
void Start() {
thisPerson = new Person("Name");
}
}
public class Requestor: MonoBehaviour {
public Person thisPerson;
void Start() {
thisPerson = new Person("Name");
}
}
public class Person {
public Quest quest;
void SetQuest(Quest quest) {
this.quest = quest;
}
}
有什么建议为什么会出错?
【问题讨论】:
-
不,那个问题只是关于 NullReferenceException,我有一个关于 C# 中的问题,特别是 Unity 中的问题。再加上这个问题已经在两年前得到回答......
-
NullReferenceException 的原因以及如何修复它们总是相同的,至少参考问题解释的原因足够好,应该清楚为什么会发生错误(这里是 requestor 或 thisPerson空)。
-
如果您费心阅读下面选定的答案,您会知道 Unity 以某种随机顺序执行操作,当使用 Start() 方法而不是 Awake() 方法时,这可能会导致 NRE,它是不是因为您的“重复”中所述的任何原因。
标签: c# unity3d nullreferenceexception