Search Unity

Lobby manager problem - Player Object running Start() and Awake() before gamescene .

Discussion in 'Multiplayer' started by SpaceMidget75, Feb 28, 2016.

  1. SpaceMidget75

    SpaceMidget75

    Joined:
    Feb 7, 2014
    Posts:
    21
    I had my game working fine with the networkmanager so decided to now move to using the lobby asset. With network manager I create my players in this method:

    Code (CSharp):
    1.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2.     {
    3.         if (conn.connectionId == 0)
    4.         {
    5.             Vector3 pos = new Vector3(-8.5f, 0, 0);
    6.             Quaternion rot = Quaternion.Euler(new Vector3(0, 0, -90));
    7.             GameObject player = (GameObject)Instantiate(playerPrefab, pos, rot);
    8.             player.name = "Player0";
    9.             ShipScript shipScript = player.GetComponent<ShipScript>();
    10.             shipScript.Player = conn.connectionId;
    11.             ShipScript existingPlayer = GameManagerScript.Players.Find(x => x.Player == conn.connectionId);
    12.             if (existingPlayer == null)
    13.                 GameManagerScript.Players.Add(shipScript);
    14.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);          
    15.         }
    16.         else
    17.         {
    18.             Vector3 pos = new Vector3(8.5f, 0, 0);
    19.             Quaternion rot = Quaternion.Euler(new Vector3(0, 0, 90));
    20.             GameObject player = (GameObject)Instantiate(playerPrefab, pos, rot);
    21.             player.name = "Player1";
    22.             ShipScript shipScript = player.GetComponent<ShipScript>();
    23.             shipScript.Player = conn.connectionId;
    24.             ShipScript existingPlayer = GameManagerScript.Players.Find(x => x.Player == conn.connectionId);
    25.             if (existingPlayer == null)
    26.                 GameManagerScript.Players.Add(shipScript);
    27.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    28.             //GameObject.Find("GameManager").GetComponent<GameManagerScript>().Active = true;
    29.         }          
    30.     }
    Lobby manager doesn't appear to have an OnServerAddPlayer method so I've used this instead:

    Code (CSharp):
    1.         public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    2.         {
    3.          
    4.             if (conn.connectionId == 0)
    5.             {
    6.                 Vector3 pos = new Vector3(-8.5f, 0, 0);
    7.                 Quaternion rot = Quaternion.Euler(new Vector3(0, 0, -90));
    8.                 GameObject player = (GameObject)Instantiate(playerPrefab, pos, rot);
    9.                 player.name = "Player0";
    10.                 ShipScript shipScript = player.GetComponent<ShipScript>();
    11.                 shipScript.Player = conn.connectionId;
    12.                 ShipScript existingPlayer = GameManagerScript.Players.Find(x => x.Player == conn.connectionId);
    13.                 if (existingPlayer == null)
    14.                     GameManagerScript.Players.Add(shipScript);
    15.                 //NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    16.             return player;
    17.             }
    18.             else
    19.             {
    20.                 Vector3 pos = new Vector3(8.5f, 0, 0);
    21.                 Quaternion rot = Quaternion.Euler(new Vector3(0, 0, 90));
    22.                 GameObject player = (GameObject)Instantiate(playerPrefab, pos, rot);
    23.                 player.name = "Player1";
    24.                 ShipScript shipScript = player.GetComponent<ShipScript>();
    25.                 shipScript.Player = conn.connectionId;
    26.                 ShipScript existingPlayer = GameManagerScript.Players.Find(x => x.Player == conn.connectionId);
    27.                 if (existingPlayer == null)
    28.                     GameManagerScript.Players.Add(shipScript);
    29.                 //NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    30.                 //GameObject.Find("GameManager").GetComponent<GameManagerScript>().Active = true;
    31.             return player;
    32.             }
    33.         }
    First seems to work fine. The problem is that I have code in my ShipScript in Awake() and/or Start() that requires components in the gamescene. In the old version, this was fine and it would find the objects but with the lobby it's not working and i get null reference on the find. Presumably, the player objects are created just before the scene is loaded?

    I imagine it's pretty important for gameobjects in the gamescene to be able to run their Start() within that actual scene. Am I doing something wrong?