[SOLVED]Object reference not set to an instance of an object C#

Hi Everyone

So Im having a slight issue finding the problem with my script everything seems fine but im getting “object reference not set to an instance of an object” error.

The script get attached to the main camera and works from there, so im not sure what unity wants from me :confused:

Am I not giving it enough attention or something?

using UnityEngine;
using System.Collections;

public class CreateNewCharacter : MonoBehaviour {

    private BasePlayer newPlayer;
    private bool isMageClass;
    private bool isWarriorClass;
    private string playerName = "EnterName";

    // Use this for initialization
    void Start () {
        newPlayer = new BasePlayer ();
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    void OnGUI(){
        playerName = GUILayout.TextArea (playerName);
        isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
        isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
        if (GUILayout.Button ("Create"))
        if (isMageClass) {
            newPlayer.PlayerClass = new BaseMageClass ();
        } else if (isWarriorClass) {
            newPlayer.PlayerClass = new BaseWarriorClass();
        }
        newPlayer.PlayerLevel = 1;
        newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
        newPlayer.Endurance = newPlayer.PlayerClass.Endurance;
        newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
        newPlayer.Strenght = newPlayer.PlayerClass.Strength;
        newPlayer.PlayerName = playerName;
        StoreNewPlayerInfo ();
        SaveInformation.SaveAllInformation ();

        if(GUILayout.Button("LOAD")){
            Application.LoadLevel("Test");
        }
    }


    private void StoreNewPlayerInfo(){
        GameInformation.PlayerName = newPlayer.PlayerName;
        GameInformation.PlayerLevel = newPlayer.PlayerLevel;
        GameInformation.Stamina = newPlayer.Stamina;
        GameInformation.Endurance = newPlayer.Endurance;
        GameInformation.Intellect = newPlayer.Intellect;
        GameInformation.Strength = newPlayer.Strenght;
    }
}

The error itself will usually tell you specifically what’s missing, as well as what line of your script the error occurred on. Without this information, there’s little anyone else will be able to do to assist you, aside from pointing out obvious things, such as you have Strength spelled two different ways in various places, which may or may not be the problem depending on what the error says… In short, please provide a bit more information. Thanks!

My apologies…
Below is the following error.

NullReferenceException: Object reference not set to an instance of an object
CreateNewCharacter.OnGUI () (at Assets/Scripts/Base Player/CreateNewCharacter.cs:32)

Also i fixed the typo on all my scripts and that wasnt the problem.
thanks if you are able to help.

Okay, looks like it’s grumbling about this line:

newPlayer.Stamina = newPlayer.PlayerClass.Stamina;

so… first, are you sure the newPlayer = BasePlayer line is working?
If so, do BasePlayer, BaseMageClass, and BaseWarriorClass all have “Stamina” (etc) properly defined?
Somewhere in there, the script just isn’t finding what it’s being told to go and get, so check the bits and bobs that are being referred to and make sure everything’s in order.

Just had a look through all of them in order and everything looks like it’s there.

Ill drop the script’s in so you can have a look if you want. Maybe I just need a second set of eyes :confused:

Ill drop then in the order i created them and im also adding the updated createnewcharacter script with the fixed typo’s.
if all els fail maybe unity just hate’s me for attempting an RPG :smile:

using UnityEngine;
using System.Collections;

public class BasePlayer {

    private string playerName;
    private int playerLevel;
    private BaseCharacterClass playerClass;
    private int stamina;
    private int endurance;
    private int intellect;
    private int strength;

    public string PlayerName{
        get{return playerName;}
        set{playerName = value;}
    }
    public int PlayerLevel{
        get{return playerLevel;}
        set{playerLevel = value;}
    }
    public BaseCharacterClass PlayerClass{
        get{return playerClass;}
        set{playerClass = value;}
    }
    public int Stamina{
        get{return stamina;}
        set{stamina = value;}
    }
    public int Endurance{
        get{return endurance;}
        set{endurance = value;}
    }
    public int Intellect{
        get{return intellect;}
        set{intellect = value;}
    }
    public int Strength{
        get{return strength;}
        set{strength = value;}
    }
}
using UnityEngine;
using System.Collections;

public class BaseWarriorClass : BaseCharacterClass {

    public BaseWarriorClass(){
        CharacterClassName = "Warrior";
        CharacterClassDescription = "A warrior, master of the sword.";
        Stamina = 15;
        Endurance = 12;
        Strength = 14;
        Intellect = 10;
    }

}
using UnityEngine;
using System.Collections;

public class BaseMageClass : BaseCharacterClass {

    public BaseMageClass(){
        CharacterClassName = "Mage";
        CharacterClassDescription = "Master of magic.";
        Stamina = 12;
        Endurance = 14;
        Strength = 10;
        Intellect = 15;
    }

}
using UnityEngine;
using System.Collections;

public class CreateNewCharacter : MonoBehaviour {

    private BasePlayer newPlayer;
    private bool isMageClass;
    private bool isWarriorClass;
    private string playerName = "EnterName";

    // Use this for initialization
    void Start () {
        newPlayer = new BasePlayer ();
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    void OnGUI(){
        playerName = GUILayout.TextArea (playerName);
        isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
        isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
        if (GUILayout.Button ("Create"))
        if (isMageClass) {
            newPlayer.PlayerClass = new BaseMageClass ();
        } else if (isWarriorClass) {
            newPlayer.PlayerClass = new BaseWarriorClass();
        }
        newPlayer.PlayerLevel = 1;
        newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
        newPlayer.Endurance = newPlayer.PlayerClass.Endurance;
        newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
        newPlayer.Strength = newPlayer.PlayerClass.Strength;
        newPlayer.PlayerName = playerName;
        StoreNewPlayerInfo ();
        SaveInformation.SaveAllInformation ();

        if(GUILayout.Button("LOAD")){
            Application.LoadLevel("Test");
        }
    }


    private void StoreNewPlayerInfo(){
        GameInformation.PlayerName = newPlayer.PlayerName;
        GameInformation.PlayerLevel = newPlayer.PlayerLevel;
        GameInformation.Stamina = newPlayer.Stamina;
        GameInformation.Endurance = newPlayer.Endurance;
        GameInformation.Intellect = newPlayer.Intellect;
        GameInformation.Strength = newPlayer.Strength;
    }
}

hmmm well update I found that // out the newPlayer lines below PlayerLevel make’s it work.

void OnGUI(){
        playerName = GUILayout.TextArea (playerName);
        isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
        isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
        if (GUILayout.Button ("Create"))
        if (isMageClass) {
            newPlayer.PlayerClass = new BaseMageClass ();
        } else if (isWarriorClass) {
            newPlayer.PlayerClass = new BaseWarriorClass();
        }
        newPlayer.PlayerLevel = 1;
        //newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
        //newPlayer.Endurance = newPlayer.PlayerClass.Endurance;
        //newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
        //newPlayer.Strength = newPlayer.PlayerClass.Strength;
        //newPlayer.PlayerName = playerName;
        StoreNewPlayerInfo ();
        SaveInformation.SaveAllInformation ();

Still cant find the reason why its isnt working though :confused:
newPlayer is working though as newPlayer.playerLevel isnt giving an error

I can’t say anything for sure, but it looks like you’re already on the right track to debugging it. I’d add some Debug lines and check things one by one. For instance:

//newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
Debug.Log("Stamina = " + newPlayer.PlayerClass.Stamina);

This way you’ll be able to tell what values, if any, are being passed into your new variables.

aye added a debug but its not showing anything. The error pops up directly after pressing play. will have a looks and check if i can find what’s causing it.

HAHA Found the problem :smile:

void OnGUI(){
        isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
        isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
        if (GUILayout.Button ("Create")){
            if(isMageClass){
                newPlayer.PlayerClass = new BaseMageClass();
            }else if(isWarriorClass){
                newPlayer.PlayerClass = new BaseWarriorClass();
            }
            newPlayer.PlayerLevel = 1;
            newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
            newPlayer.Endurance = newPlayer.PlayerClass.Endurance;
            newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
            newPlayer.Strength = newPlayer.PlayerClass.Strength;

            Debug.Log("Player Class" + newPlayer.PlayerClass.CharacterClassName);
            Debug.Log("Player Level" + newPlayer.PlayerLevel);
            Debug.Log("Player Stamina" + newPlayer.Stamina);
            Debug.Log("Player Endurance" + newPlayer.Endurance);
            Debug.Log("Player Intellect" + newPlayer.Intellect);
            Debug.Log("Player Strength" + newPlayer.Strength);
        }

Didnt have them nested with the if statements.

Right on :wink: Don’t you love/hate it when it’s something simple? :stuck_out_tongue: Have fun!