[英]NullReferenceException in Unity (C#)
I'm trying to add a Quest-object to a Person. 我正在尝试将一个搜索对象添加到一个人。 It succeeds for one and gives a nullreferenceexception for the other, what am I doing wrong here?
它为一个成功,为另一个提供nullreferenceexception,我在这里做错了什么? PS The player and requestor are set in the Unity inspector.
PS播放器和请求者在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;
}
}
Any suggestions why this is going wrong? 有什么建议为什么会出错吗?
Move your variable initialization in to Awake()
, see the documentation for the following (paraphrased): 将变量初始化移到
Awake()
,请参阅以下文档 (措辞):
Awake is used to initialize any variables or game state before the game starts.... and use Start to pass any information back and forth.
Awake用于在游戏开始之前初始化任何变量或游戏状态....,并使用Start来回传递任何信息。
The way your GameCreator.Start()
is written you are reliant on the arbitrary order in which Unity calls your scripts. GameCreator.Start()
的编写方式GameCreator.Start()
Unity调用脚本的任意顺序。 GameCreator
could be the first object called, in which case none of your other scripts have initialized their values. GameCreator
可能是第一个被调用的对象,在这种情况下,您的其他脚本都没有初始化它们的值。
Other possible errors: 其他可能的错误:
requestor
, I'm going to assume this was done in Unity's Inspector. requestor
,我将假设这是在Unity的Inspector中完成的。 As Jordak said, your Start methods can run in any possible order, so you can't rely on Start of some component in the other. 正如Jordak所说,您的Start方法可以按任何可能的顺序运行,因此您不能依赖另一个组件中的Start组件。 You have several ways to address this issue:
您可以通过多种方式解决此问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.