Need help with code

using UnityEngine;
using System.Collections;

public class CreateNewCharacter : MonoBehaviour {

    private BasePlayer newPlayer;
    private bool isSamuraiClass ;
    private bool isVampireClass;
    // Use this for initialization
    void Start () {
        newPlayer = new BasePlayer ();
        //newPlayer.PlayerClass = new BaseCharacterSamurai();
    }
   
    // Update is called once per frame
    void Update () {
        Debug.Log (newPlayer.Damage);
    }

    void OnGUI(){
        isSamuraiClass = GUILayout.Toggle (isSamuraiClass, "Samurai Class");
        isVampireClass = GUILayout.Toggle (isVampireClass, "Vampire Class");
        if (GUILayout.Button ("Create")) {
            if (isSamuraiClass) {
                newPlayer.PlayerClass = new BaseCharacterSamurai ();
            } else if (isVampireClass)
                newPlayer.PlayerClass = new BaseCharacterVampire ();
        }
   
        newPlayer.PlayerLevel = 1;
        newPlayer.Damage = newPlayer.PlayerClass.Damage;
}
}

The moment i add the line newPlayer.Damage = newPlayer.PlayerClass.Damage at the end I get NullReferenceException and my GUI buttons in the scene dont show up anymore. I excpected to be able to set the PlayerClass with my GUI, so no wonder that console complains about newPlayer.PlayerClass.Damage. I pretty much copied this from an online tutorial and just cant find where i mess up.If I add newPlayer.PlayerClass = new BaseCharacterSamurai (); in void start it works.So why i cannot do this step in my scene with the toggles ( why dont they appear correctly anymore?) Pls help

Move that code into the if statement above it.

1 Like

Thanks! :slight_smile: