I am new in Unity/C# and i cant find a answer for my problem :0
I have a script(selectCharacter) which adds a script (Knight) to a gameobject. The Knight-script calls the method "SetResource(true)". This method creates a NullReferenceException. The problem is generate by the attribute "private Attribute health" (Attribute is a non-Monobehaviour class)but i dont know why, because went i but the knight-script manuel to the gameobject everything is fine.
Thanks for help and sorry for my bad english.
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>();
}
}