Question by
KochamMaslo · Sep 10, 2016 at 08:04 AM ·
nullreferenceexception
A problem "NullReferenceException: Object reference not set to an instance of an object(...)"
Hello. I have a problem: "NullReferenceException: Object reference not set to an instance of an object CreateNewCharacter.OnGUI () (at Assets/Scripts/Create Player/CreateNewCharacter.cs:30)". My codes:
CreateNewCharacter:
using UnityEngine;
using System.Collections;
public class CreateNewCharacter : MonoBehaviour {
private BasePlayer newPlayer;
private bool isMageClass;
private bool isWarriorClass;
void Start () {
newPlayer = new BasePlayer();
}
void Update () {
}
void OnGUI(){
isMageClass = GUILayout.Toggle (isMageClass, "Mag");
isMageClass = GUILayout.Toggle (isWarriorClass, "Wojownik");
if (GUILayout.Button ("Stwórz")) {
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("Klasa postaci: " + newPlayer.PlayerClass.CharacterClassName);
Debug.Log("Poziom postaci: " + newPlayer.PlayerLevel);
Debug.Log("Poziom staminy: " + newPlayer.Stamina);
Debug.Log("Poziom wytrzymałości: " + newPlayer.Endurance);
Debug.Log("Poziom intelektu: " + newPlayer.Intellect);
Debug.Log("Poziom siły: " + newPlayer.Strength);
}
}
}
BasePlayer:
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; }
}
}
I have Unity 5. But maybe in the Unity 5 changed some commands ? Please help ^^ :-(
And i have a problem with private bool isWarriorClass. MonoDevelop is telling me that there isnt any value and this will be 'false'. Its not a red error but orange error: "Assets/Scripts/Create Player/CreateNewCharacter.cs(8,22): warning CS0649: Field `CreateNewCharacter.isWarriorClass' is never assigned to, and will always have its default value `false' " Maybe it's a problem?
But the tutorial that watch was created in 2014 , so if I know , then it was unity 4. It can either command has been changed in version 5 ?
Comment