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