- Home /
NullReference - Object Instantiated but still null
Hello and I am trying to load a character into a game scene by instanciating a new player. But when I try to load him, I get a null reference exception. Here is my MasterClient.cs, which has the error at line 88 or
newPlayer.GetComponent<Player>().Load (PlayerLoadUpdateName, PlayerLoadUpdateInfo, PlayerLoadUpdatePosition);
(just separating the code here)
using UnityEngine;
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class MasterClient : MonoBehaviour
{
public static MasterClient Singleton;
public GameObject PlayerObj;
#region Networking Variables
private const int PORT = 5055;
private static TcpClient Client;
private static NetworkStream Stream;
private static BinaryWriter ClientWriter;
private static BinaryReader ClientReader;
private static DataOutputStream ClientOutput;
private static DataInputStream ClientInput;
private static PacketEncoder PacketsOut;
private static PacketDecoder PacketsIn;
#endregion
private static Thread inputListener;
void Awake()
{
if (Singleton == null)
{
Singleton = this;
DontDestroyOnLoad(this);
}
else
{
Destroy(gameObject);
}
}
void Start()
{
if (Client == null)
{
Connect();
}
}
void Update()
{
if (SceneUpdateRequired)
{
UpdateScene();
}
if(PlayerLoadUpdateRequired)
{
UpdateCharacterLoad();
}
}
bool PlayerLoadUpdateRequired = false;
string PlayerLoadUpdateName = "";
int[] PlayerLoadUpdateInfo = new int[9];
Vector3 PlayerLoadUpdatePosition = Vector3.zero;
public void ApplyCharacterLoadUpdate(string name,int[] info, Vector3 position)
{
PlayerLoadUpdateName = name;
PlayerLoadUpdateInfo = info;
PlayerLoadUpdatePosition = position;
PlayerLoadUpdateRequired = true;
}
public void UpdateCharacterLoad()
{
SpawnPlayer();
PlayerLoadUpdateRequired = false;
}
void SpawnPlayer()
{
GameObject newPlayer = (GameObject)Instantiate (PlayerObj, PlayerLoadUpdatePosition, new Quaternion ());
newPlayer.GetComponent<Player>().Load (PlayerLoadUpdateName, PlayerLoadUpdateInfo, PlayerLoadUpdatePosition);
}
public void Connect()
{
Client = new TcpClient ();
try {
Client.Connect (IPAddress.Parse ("127.0.0.1"), PORT);
Stream = Client.GetStream();
ClientWriter = new BinaryWriter(Stream);
ClientReader = new BinaryReader(Stream);
ClientOutput = new DataOutputStream(ClientWriter);
ClientInput = new DataInputStream(ClientReader);
PacketsOut = new PacketEncoder(ClientOutput);
PacketsIn = new PacketDecoder(this, ClientInput);
inputListener = new Thread(new ThreadStart(InputLoop));
inputListener.Start();
} catch (Exception e) {
Debug.LogException (e);
}
}
public void SendPacket(int packetID, string Data)
{
string[] packetData = Data.Split ('~');
switch (packetID)
{
case 0: //login
PacketsOut.SendLogin(packetData[0], packetData[1]);
break;
case 1: //Character Request
PacketsOut.SendCharacterRequest();
break;
case 2: //Create a Character
int bodyColor = int.Parse(packetData[0]);
int eyeColor = int.Parse(packetData[1]);
PacketsOut.SendCharacterCreationRequest(bodyColor, eyeColor, packetData[2]);
break;
case 3: //Load Character
PacketsOut.SendCharacterLoadRequest(packetData[0]);
break;
}
}
void InputLoop()
{
while (Client.Connected)
{
int PacketID = ClientInput.ReadInt();
switch(PacketID)
{
case 0: //Login
PacketsIn.Login();
break;
case 1: //Character Request Responce
PacketsIn.CharacterRequest();
break;
case 2: //Character Creation Responce
PacketsIn.CharacterCreate();
break;
case 3: //Load Character
PacketsIn.LoadCharacter();
break;
}
}
}
private bool SceneUpdateRequired = false;
private int SceneUpdateID = 0;
public void ChangeScene(int sceneID)
{
SceneUpdateID = sceneID;
SceneUpdateRequired = true;
}
private void UpdateScene()
{
Application.LoadLevel (SceneUpdateID);
SceneUpdateRequired = false;
}
}
and my Player.cs class that has the .Load() method:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public static Player instance;
private string PlayerName;
private int PlayerLevel;
private int CurrentHealth, MaximumHealth;
private int CurrentExperience, MaximumExperience;
private int Strength, Agility, Intellect, Constitution;
private Vector3 CurrentPosition;
void Start()
{
instance = this;
}
void Update()
{
}
public void Load(string PlayerLoadUpdateName, int[] PlayerLoadUpdateInfo, Vector3 PlayerLoadUpdatePosition)
{
PlayerName = PlayerLoadUpdateName;
PlayerLevel = PlayerLoadUpdateInfo [0];
CurrentHealth = PlayerLoadUpdateInfo [1];
MaximumHealth = PlayerLoadUpdateInfo [2];
CurrentExperience = PlayerLoadUpdateInfo [3];
MaximumExperience = PlayerLoadUpdateInfo [4];
Strength = PlayerLoadUpdateInfo [5];
Agility = PlayerLoadUpdateInfo [6];
Intellect = PlayerLoadUpdateInfo [7];
Constitution = PlayerLoadUpdateInfo [8];
CurrentPosition = PlayerLoadUpdatePosition;
}
}
I tried to debug the newPlayer object but it says it is an object and doesn't return null. But then it tries to load him and it is null.
I don't have Unity in front of me so I can't be sure but I'm guessing that typecasting is your problem, have you tried using:
GameObject newPlayer = Instantiate (PlayerObj, PlayerLoadUpdatePosition, Quaternion.identity) as GameObject;
Answer by SkaredCreations · Dec 13, 2014 at 02:21 PM
The information about the exception is confusing, are you getting the exception at line 88 or 89? The console log should say the line. If it's at line 88 then it seems like PlayerObj is not set in the hierarchy panel, if it's at line 89 then it seems like the player prefab hasn't the component Player attached. Anyway you should use "Quaternion.identity" and not "new Quaternion()" in the Instantiate call.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullReferenceException script problem? 0 Answers
[Roll-a-ball] Changing UI text (NRE) 0 Answers
[Solved] Actually totally explainable NRE with 2D array 0 Answers