Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cockaoo2X · Dec 13, 2014 at 11:11 AM · c#nullreferenceexception

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CorruptedTNC · Dec 13, 2014 at 02:10 PM 0
Share

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;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges