Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting null Exception when I try to spawn player

Discussion in 'Multiplayer' started by tylerreece22, Nov 20, 2015.

  1. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    Here is my code. For some weird reason I am getting a null exception every time I try to spawn my player.

    Thank you for the help in advance :)

    Code (CSharp):
    1. private void SpawnPlayer() {
    2.         Debug.Log ("Spawning Player...");
    3.         Network.Instantiate (Resources.Load ("Resources/SkeletonPlayer"), new Vector3 (6f, 0.6f, 4.5f), Quaternion.identity, 0);
    4.     }
    5.  
    6.  
    7.     void OnServerInitialized() {
    8.         Debug.Log ("Server has been initialized");
    9.         SpawnPlayer ();
    10.     }
     
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    First you instantiate. Then you spawn the instance of the prefab (not the prefab).
    http://docs.unity3d.com/Manual/UNetSpawning.html

    GameObject tree = (GameObject)Instantiate(treePrefab, transform.position, transform.rotation); NetworkServer.Spawn(tree);
     
  3. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    thanks so much!!!! that was perfect!!!! Now I am running into another issue though. I try to instantiate my prefab but it tells be there is no connection to the server. But the this is I run the code below and it gets through my nests if statements every time. The most odd part is that everything seems to work until I get to my instantiate.

    Code (csharp):
    1. public void SetupServer()
    2.     {
    3.         NetworkServer.Listen(4444);
    4.         isAtStartup = false;
    5.         int x = 0;
    6.         while (x == 0)
    7.         {
    8.             if (NetworkServer.active)
    9.             {
    10.                 Debug.Log("Server Connected");
    11.                 x = 1;
    12.             }
    13.         }
    14.     }
    15.  
    16. public void SetupClient()
    17.     {
    18.         myClient = new NetworkClient();
    19.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    20.         myClient.Connect("127.0.0.1", 4444);
    21.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    22.         ClientScene.RegisterPrefab(playerPrefab);
    23.  
    24.         isAtStartup = false;      
    25.     }
    26.  
    27.     // Create a local client and connect to the local server
    28.     public void SetupLocalClient()
    29.     {
    30.         myClient = ClientScene.ConnectLocalServer();
    31.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    32.         isAtStartup = false;
    33.     }
    34.  
    35.     // client function
    36.     public void OnConnected(NetworkMessage netMsg)
    37.     {
    38.         Debug.Log("Connected to server");
    39.         int x = 0;
    40.         while (x == 0)
    41.         {
    42.             if (myClient.isConnected)
    43.             {
    44.                 Network.Instantiate(playerPrefab, new Vector3(6f, 0.6f, 4.5f), Quaternion.identity, 0);
    45.                 x = 1;
    46.             }
    47.         }
    48.     }