Trying to spawn a specific prefab when lobby scene transitions to game scene

  //Called on server.
  //This allows customization of the creation of the GamePlayer object on the server.
  //By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour
  //to be customized.The object returned from the function will be used to replace the lobby-player on the connection.
  public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
  {
    //default player prefab index
    int prefabIndex = 1;

    //For each lobby player in scene
    foreach (LobbyPlayer lp in FindObjectsOfType<LobbyPlayer>())
    {
      //If connection contains lobby player network identity
      if (conn.clientOwnedObjects.Contains(lp.GetComponent<NetworkIdentity>().netId))
      {
        //then set prefab index to spawn value
        prefabIndex = lp.spawnTypeValue;
      }
    }

    //Spawn game object from spawn list
    GameObject playerPrefab = Instantiate(spawnPrefabs[prefabIndex]);
    return playerPrefab;
  }

This works fine when I test he game with one player. When there is two players it tells me that conn.clientOwnedObjects is null. Is there a better way to find the lobby player on the server that is used by that specific connection?

bump

In the game I am working on, the players join a lobby and select a “champion” they want to take into battle. When all the players have selected ready, it transitions into the game scene and spawns the prefab for the type of “champion” the player had selected in the lobby.

I accomplish this by keeping the ID number of the “champion” sync’d in the lobby player object, and then that ID is passed to the game player object, which is then spawned by that game player script.

http://docs.unity3d.com/ScriptReference/Networking.NetworkLobbyManager.OnLobbyServerSceneLoadedForPlayer.html

The above method is what I use to transfer the ID’s from the lobbyPlayer to the gamePlayer objects. Hopefully this helps, if not I can give some code as example.

That’s probably what I should have done, but I did figure out a way to spawn a specific prefab based on a selection on the lobby player if anyone else is trying to do the same thing.

  //Called on server.
  //This allows customization of the creation of the GamePlayer object on the server.
  //By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour
  //to be customized.The object returned from the function will be used to replace the lobby-player on the connection.
  public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
  {
    //default player prefab index
    int prefabIndex = 1;

    //For each lobby player in scene
    foreach (LobbyPlayer lp in FindObjectsOfType<LobbyPlayer>())
    {
      int connectionID = 0;
      NetworkIdentity ni = lp.GetComponent<NetworkIdentity>();

      //Get connection id from network identity.
      //The connection id value depends on whether its a client player or server player
      if (ni.connectionToClient != null)
        connectionID = ni.connectionToClient.connectionId;
      else if (ni.connectionToServer != null)
        connectionID = ni.connectionToServer.connectionId;

      //If connection id on lobby player is same as connection id
      if (connectionID == conn.connectionId)
      {
         //then set prefab index to spawn value
        prefabIndex = lp.spawnTypeValue;
      }
    }

    //Spawn game object from spawn list
    GameObject playerPrefab = Instantiate(spawnPrefabs[prefabIndex]);
    return playerPrefab;
  }

This code uses a network identity compared to the connection to find what is owned by that connection.

    foreach (NetworkIdentity ni in FindObjectsOfType<NetworkIdentity>())
    {
      int connectionID = 0;

      if (ni.connectionToClient != null)
        connectionID = ni.connectionToClient.connectionId;
      else if (ni.connectionToServer != null)
        connectionID = ni.connectionToServer.connectionId;

      if (connectionID == conn.connectionId)
      {
        //This gameobject is owned by this connection.
      }
    }

If you could share your scripts, or more hopefully a small project example, that would be just wonderful. Please.