- Home /
 
nullreference with getcomponent
Hi! as i'm trying to understand how networking works I followed few tutos for making an authoritative server and I managed to make client connect and spawn their characters.
But I get a "NullReferenceException: Object reference not set to an instance of an object" when
playerScripts.Add (myNewTrans.GetComponent ());
this is on server side
 public void SpawnNewPlayer(NetworkPlayer newPlayer)
     {
         Debug.Log("coucou");
         int playerNumber = int.Parse (newPlayer + "");
         Debug.Log(int.Parse(newPlayer + ""));
 
         GameObject myNewTrans = Network.Instantiate(Player, Startpos, transform.rotation, playerNumber) as GameObject;
         
         playerScripts.Add (myNewTrans.GetComponent<PlayerController> ());
         
         NetworkView newObjectsNetworkview = myNewTrans.GetComponent<NetworkView>();
         
         newObjectsNetworkview.RPC ("SetPlayer", RPCMode.AllBuffered, newPlayer);
 
     }
 
               since I feel kinda like a noob, I was hoping someone could explain me what's going wrong.
On top of the class
  public ArrayList playerScripts = new ArrayList ();
 
                  With debug I was able to find that this is myNewTrans thas is null, but I still don't know why.
Are you sure the result of that function is a GameObject?... according to this, that function returns an Object, not a GameObject:
http://docs.unity3d.com/ScriptReference/Network.Instantiate.html
Answer by Elkurion · Apr 15, 2015 at 05:09 PM
I found it! Thank you.
so:
  GameObject myNewTrans = Network.Instantiate(Player, Startpos, transform.rotation, playerNumber) as GameObject;
 
               should be:
  Transform myNewTrans = Network.Instantiate(Player, Startpos, transform.rotation, playerNumber) as Transform;
 
              Your answer
 
             Follow this Question
Related Questions
how to make several scenes with one server? 1 Answer
Dedicated server lobby problem 0 Answers
Unity networking tutorial? 6 Answers
Network.Instantiate dont instantiate object 0 Answers
Spawning Clients 1 Answer