Click here to Skip to main content
15,799,398 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys, i have a problem with a code, practically when i press the button " create " i should print some information, but it's like my code cannot find those information.
Here's a code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CreateNewCharacter : MonoBehaviour {

	private BasePlayer newPlayer;
	private string playerName = "Enter Name";
	Text charText;
	private bool soldier = true;
	private bool pilot;
	private bool citizen;
	private bool create;

	// Use this for initialization

	void Awake(){


	void Start () {

	// Update is called once per frame
	void Update () {


	void OnGUI()
		create = GUI.Button(new Rect((Screen.width/2),(Screen.height/2)+25,100,20),"Create");
		playerName = GUI.TextArea(new Rect((Screen.width/2)-100,(Screen.height/2)-25,200,25),playerName ,15);//limited to 15 letters
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2),100,20),soldier, "Soldier")) {
			soldier = true;
			pilot = false;
			citizen = false;
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2)+25,100,20),pilot, "Pilot")) {
			pilot = true;
			soldier = false;
			citizen = false;
		if (GUI.Toggle(new Rect((Screen.width/2)-100,(Screen.height/2)+50,100,20),citizen, "Citizen")) {
			pilot = false;
			soldier = false;
			citizen = true;

		if (create == true){
			Debug.Log("Button was pressed");
				newPlayer.PlayerClass = new BaseSoldierClass();
			}else if(pilot){
				newPlayer.PlayerClass = new BasePilotClass();
			}else if(citizen){
				newPlayer.PlayerClass = new BaseCitizenClass();
			newPlayer.PlayerLevel = 1;
			newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
			newPlayer.Velocity = newPlayer.PlayerClass.Velocity;
			newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
			newPlayer.Strength = newPlayer.PlayerClass.Strength;
			newPlayer.Observation = newPlayer.PlayerClass.Observation;
			newPlayer.Luck = newPlayer.PlayerClass.Luck;
			newPlayer.PlayerName = playerName;
			Debug.Log("Player Name : " + newPlayer.PlayerName);
			Debug.Log("Player Class : " + newPlayer.PlayerClass.CharacterClassName);
			Debug.Log("Player Level : " + newPlayer.PlayerLevel);
			Debug.Log("Player Stamina : " + newPlayer.Stamina);
			Debug.Log("Player Velocity : " + newPlayer.Velocity);
			Debug.Log("Player Intellect : " + newPlayer.Intellect);
			Debug.Log("Player Strength : " + newPlayer.Strength);
			Debug.Log("Player Observation : " + newPlayer.Observation);
			Debug.Log("Player Luck : " + newPlayer.Luck);


using UnityEngine;
using System.Collections;

public class BaseSoldierClass : BaseCharacterClass {
	public BaseSoldierClass() {
		CharacterClassName = "Soldato";
		CharacterClassDescription = "E' un soldato addestrato per combattere i gay";
		Stamina = 15;
		Velocity = 15;
		Strength = 20;
		Intellect = 10;
		Observation = 10;
		Luck = 10;

someone know how to fix it? the error is this one
NullReferenceException: Object reference not set to an instance of an object
CreateNewCharacter.OnGUI () (at Assets/Scripts/CreateNewCharacter.cs:61)
Member 11452782 15-Feb-15 0:35am    
i read all link but still i cannot found the solution, i think i initialize the variable with "private BasePlayer newPlayer;". Can You write the correct solution so maybe i'll understand with an example.

1 solution

That means you need to initialize the variable other than declaring it only. It is a very basic error that new developers get into. Which so ever object throws this exception, you need to give it a value (initialize it), so that the value must be evaluated.[^] MSDN has a great information about this exception and when would it get thrown. You need to read that document so that you can understand the underlying problem.

A few more previous posts would also help you out:

How to resolve NullReferenceException[^] (See answer from Sergey Alexandrovich Kryukov).[^][^] A thread of threads about similar exceptions on Unity.
Share this answer
Sergey Alexandrovich Kryukov 14-Feb-15 23:37pm    
Sure, a 5.
Afzaal Ahmad Zeeshan 14-Feb-15 23:39pm    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900