getting a NullReferenceException error and can't see why.
Been following along with a tutorial on YouTube by a guy named HardlyBriefDan. He's doing a tutorial on how to create a basic RPG game. The guy seems to know what he's doing and I'm right there with him doing all he does, but I'm getting an error. I tried posting on the video page, messaging the guy directly, but not getting any help. Was suggested I ask here, so here it is.
The error:
NullReferenceException: Object reference not set to an instance of an object
The Script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class CreateNewCharacter : MonoBehaviour {
private BasePlayer newPlayer;
private bool isMageClass;
private bool isWarriorClass;
private bool isRogueClass;
private bool isClercClass;
private bool isRangerClass;
private bool isDruidClass;
private string playerName;
// Use this for initialization
void Start () {
newPlayer = new BasePlayer();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
// creates a name and limits it to 15 letters
playerName = GUILayout.TextArea(playerName, 15);
// choose your class
isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
isRogueClass = GUILayout.Toggle(isRogueClass, "Rogue Class");
isClercClass = GUILayout.Toggle(isClercClass, "Cleric Class");
isRangerClass = GUILayout.Toggle(isRangerClass, "Ranger Class");
isDruidClass = GUILayout.Toggle(isDruidClass, "Druid Class");
if (GUILayout.Button("Create"))
{
if (isMageClass) { newPlayer.PlayerClass = new BaseMageClass(); }
else if (isWarriorClass) { newPlayer.PlayerClass = new BaseWarriorClass(); }
else if (isRogueClass) { newPlayer.PlayerClass = new BaseRogueClass(); }
else if (isClercClass) { newPlayer.PlayerClass = new BaseClericClass(); }
else if (isRangerClass) { newPlayer.PlayerClass = new BaseRangerClass(); }
else if (isDruidClass) { newPlayer.PlayerClass = new BaseDruidClass(); }
// start player at level 1 and get the values set for them
newPlayer.PlayerLevel = 1;
newPlayer.Stregnth = newPlayer.PlayerClass.Stregnth;
newPlayer.Intelegence = newPlayer.PlayerClass.Intelegence;
newPlayer.Constitution = newPlayer.PlayerClass.Constitution;
newPlayer.Dexterity = newPlayer.PlayerClass.Dexterity;
newPlayer.Charisma = newPlayer.PlayerClass.Charisma;
newPlayer.Wisdom = newPlayer.PlayerClass.Wisdom;
// get player name and additional information
newPlayer.PlayerName = playerName;
StoreNewPlayerInfo();
SaveInformation.SaveAllInformation();
}
if (GUILayout.Button("Load")) { SceneManager.LoadScene("test"); }
}
// store all information on empty GameInformation Object
private void StoreNewPlayerInfo()
{
GameInformation.PlayerName = newPlayer.PlayerName;
GameInformation.PlayerLevel = newPlayer.PlayerLevel;
GameInformation.Stregnth = newPlayer.Stregnth;
GameInformation.Intelegence = newPlayer.Intelegence;
GameInformation.Constitution = newPlayer.Constitution;
GameInformation.Dexterity = newPlayer.Dexterity;
GameInformation.Charisma = newPlayer.Charisma;
GameInformation.Wisdom = newPlayer.Wisdom;
}
}
I also think the guy in the tutorial is using version 4.5 and I am using version 5.3
thanks in advance.
I have a problem with toggles throwing null reference error exception also, if it calls a function in a script it NREE's. They always work like I expect. I think it's something with Unity.
You should post the exact error message with the line number. There's about a 100 things in that script that could throw an NRE and juat by reading the error message narrows it down to a few.
You can't fix an NRE without knowing exactly which variable is the null one. Finding out that is the first step. Finding out why is the second
Answer by SirNiklas · Sep 25, 2016 at 05:42 PM
It seems that the guy you are watching tutorials from does not know what he is doing. On top the video is old, which shows itself by the use of the OnGUI method.
NoseKills already wrote it, to exactly tell you where the mistake lies I have to see the error message. My guess is that the Start method is not called before the first enter into OnGUI which leads to newPlayer being accessed although it is not referencing any object.
The boolean fields in the CreateNewCharacter type are not good practice. Rather you should use an enum which contains every class choosable from.
[Serializable]
public enum PlayerClass
{
Warrior,
Rogue,
Clerc,
Ranger,
Druid
}
Now you can use a switch to check the chosen class and create a new player from this information.
private void ChooseClass(PlayerClass class)
{
switch (class)
{
case PlayerClass.Warrior:
newPlayer = new BaseWarriorClass();
break;
// And so on
}
// Its way simpler to just store a BasePlayer reference in the GameInformation static type than to assign every important value one by one
GameInformation.player = newPlayer;
}
I declared a new method for the process of creating and setting the character because UI and game logic should normally be strictly separated.
When you do this GameInformation.player = newPlayer; how do you access information ? like the player hp example?
Your answer
Follow this Question
Related Questions
NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers
Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers