我正在嘗試向某個人添加一個Quest對象。它成功了一個,並給另一個nullreferenceexception,我在這裏做錯了什麼? P.S.播放器和請求者在Unity檢查器中設置。Unity中的NullReferenceException(C#)
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,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-修復它) – LearnCocos2D
不,那個問題只是關於一個NullReferenceException,我在Unity中專門針對C#中的人提出了一個問題。另外這個問題已經在兩年前得到了解答...... – Teysz
NullReferenceException的原因以及如何解決它們總是相同的,至少參考問題正在很好地解釋原因,應該清楚爲什麼會發生錯誤(這裏requestor或thisPerson都是null)。 – LearnCocos2D