0
我在Unity/C#新的,我不能找到我的問題的答案:0非Monobehaviour屬性創建的NullReferenceException
我有一個腳本(selectCharacter)增加了一個腳本(騎士)的遊戲對象。 騎士腳本調用方法「SetResource(true)」。此方法創建一個NullReferenceException。問題是由屬性「私人屬性健康」(屬性是一個非Monobehaviour類)生成的,但我不知道爲什麼,因爲我去了,但騎士腳本manuel到gameobject一切都很好。
感謝您的幫助和抱歉,我的英語不好。
public class SelectCharacter : MonoBehaviour {
void Start() {
gameObject.AddComponent(typeof(Knight));
}
}
public class Knight : PlayerStandardAttribute
{
void Start() {
SetResource(true)
}
}
public class PlayerStandardAttribute : MonoBehaviour {
private Attribute health;
public void SetResource(bool healthP) {
if (healthP == true){
Debug.Log(health.ToString());
health.CurrentBar = GameObject.Find("HealthBar").GetComponent<Bar>();
}
}
嘿,感謝老兄,我解決了問題,但我要創建在PlayerStandardAttribute的intance。類似這樣的:public class PlayerStandardAttribute:MonoBehaviour {0}私有屬性健康狀況{ } 私人屬性健康狀況; void Awake(){health = new Attribute();} – Omgoor