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

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

Discussion in 'Multiplayer' started by Ralem, Mar 8, 2016.

  1. Ralem

    Ralem

    Joined:
    Sep 26, 2013
    Posts:
    15
    Code (csharp):
    1.  
    2.   //Called on server.
    3.   //This allows customization of the creation of the GamePlayer object on the server.
    4.   //By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour
    5.   //to be customized.The object returned from the function will be used to replace the lobby-player on the connection.
    6.   public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    7.   {
    8.     //default player prefab index
    9.     int prefabIndex = 1;
    10.  
    11.     //For each lobby player in scene
    12.     foreach (LobbyPlayer lp in FindObjectsOfType<LobbyPlayer>())
    13.     {
    14.       //If connection contains lobby player network identity
    15.       if (conn.clientOwnedObjects.Contains(lp.GetComponent<NetworkIdentity>().netId))
    16.       {
    17.         //then set prefab index to spawn value
    18.         prefabIndex = lp.spawnTypeValue;
    19.       }
    20.     }
    21.  
    22.     //Spawn game object from spawn list
    23.     GameObject playerPrefab = Instantiate(spawnPrefabs[prefabIndex]);
    24.     return playerPrefab;
    25.   }
    26.  
    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?
     
  2. Ralem

    Ralem

    Joined:
    Sep 26, 2013
    Posts:
    15
    bump
     
  3. molx88

    molx88

    Joined:
    Apr 6, 2015
    Posts:
    6
    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/ScriptRefer...anager.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.
     
  4. Ralem

    Ralem

    Joined:
    Sep 26, 2013
    Posts:
    15
    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.
    Code (csharp):
    1.  
    2.   //Called on server.
    3.   //This allows customization of the creation of the GamePlayer object on the server.
    4.   //By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour
    5.   //to be customized.The object returned from the function will be used to replace the lobby-player on the connection.
    6.   public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    7.   {
    8.     //default player prefab index
    9.     int prefabIndex = 1;
    10.  
    11.     //For each lobby player in scene
    12.     foreach (LobbyPlayer lp in FindObjectsOfType<LobbyPlayer>())
    13.     {
    14.       int connectionID = 0;
    15.       NetworkIdentity ni = lp.GetComponent<NetworkIdentity>();
    16.  
    17.       //Get connection id from network identity.
    18.       //The connection id value depends on whether its a client player or server player
    19.       if (ni.connectionToClient != null)
    20.         connectionID = ni.connectionToClient.connectionId;
    21.       else if (ni.connectionToServer != null)
    22.         connectionID = ni.connectionToServer.connectionId;
    23.  
    24.       //If connection id on lobby player is same as connection id
    25.       if (connectionID == conn.connectionId)
    26.       {
    27.          //then set prefab index to spawn value
    28.         prefabIndex = lp.spawnTypeValue;
    29.       }
    30.     }
    31.  
    32.     //Spawn game object from spawn list
    33.     GameObject playerPrefab = Instantiate(spawnPrefabs[prefabIndex]);
    34.     return playerPrefab;
    35.   }
    36.  
    This code uses a network identity compared to the connection to find what is owned by that connection.

    Code (csharp):
    1.  
    2.     foreach (NetworkIdentity ni in FindObjectsOfType<NetworkIdentity>())
    3.     {
    4.       int connectionID = 0;
    5.  
    6.       if (ni.connectionToClient != null)
    7.         connectionID = ni.connectionToClient.connectionId;
    8.       else if (ni.connectionToServer != null)
    9.         connectionID = ni.connectionToServer.connectionId;
    10.  
    11.       if (connectionID == conn.connectionId)
    12.       {
    13.         //This gameobject is owned by this connection.
    14.       }
    15.     }
    16.  
     
  5. Deleted User

    Deleted User

    Guest

  6. jinnindo

    jinnindo

    Joined:
    Dec 8, 2015
    Posts:
    16
    If you could share your scripts, or more hopefully a small project example, that would be just wonderful. Please.