Search Unity

Network Lobby Manager Spawn Selected Prefab for Client

Discussion in 'Connected Games' started by Eiseno, Dec 28, 2015.

  1. Eiseno

    Eiseno

    Joined:
    Nov 1, 2015
    Posts:
    83
    Hi,
    I want make each client can select their player prefab in lobby and when game start i want instantiate selected prefab for client.I define varible in LobbyManager but its not working.Its always same with host.How can i make this ?

    Edit: I solved with this code.But i dont know is it good or bad way.

    Code (CSharp):
    1.   public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
    2.         {
    3.             //This hook allows you to apply state data from the lobby-player to the game-player
    4.             //just subclass "LobbyHook" and add it to the lobby object.
    5.  
    6.             if (_lobbyHooks)
    7.                 _lobbyHooks.OnLobbyServerSceneLoadedForPlayer(this, lobbyPlayer, gamePlayer);
    8.  
    9.             int prefabIndex = lobbyPlayer.GetComponent<LobbyPlayer>().prefabIndex;
    10.             Debug.LogError("Prefab Index:" + spawnPrefabs[prefabIndex].name);
    11.  
    12.             GameObject playerPrefab = (GameObject)Instantiate(spawnPrefabs[prefabIndex]);
    13.             //gamePlayer = playerPrefab;
    14.           //  NetworkServer.Spawn(playerPrefab);
    15.             NetworkServer.AddPlayerForConnection(
    16.                 lobbyPlayer.GetComponent<NetworkIdentity>().connectionToClient, playerPrefab,(short)Random.Range(0,32));
    17.             return true;
    18.         }
     
    Last edited: Dec 29, 2015
  2. J-F

    J-F

    Joined:
    Nov 8, 2012
    Posts:
    101
    Thanks for this, but when i use this method it spawns two playerobjects per client.
    Any way to fix that?
     
  3. KevinHagen

    KevinHagen

    Joined:
    Jul 5, 2017
    Posts:
    3
    Hey Dude I dont know if your problem is still recent or if youre still interested in this topic, but I have found a viable solution for that problem.

    Documentation links:
    https://docs.unity3d.com/ScriptReference/Networking.NetworkLobbyManager.html (useful overview)
    https://docs.unity3d.com/ScriptRefe...bbyManager.OnLobbyServerCreateGamePlayer.html (the method to use)
    The API doesnt get into too much detail but it was enough for me to figure out how it works. Below is a blog post by a guy who had a similiar problem and gives some more explanatory.

    https://abrgame.blogspot.de/2016/01...fferent-player.html?showComment=1467227533786

    If you have any more questions, feel free to ask me! :)
     
  4. kelkelsoh

    kelkelsoh

    Joined:
    Jun 20, 2018
    Posts:
    4
    Sorry to bumb an old thread! But I was following the link in the blogspot and found implemented it into my LobbyManager. But I encountered a weird error where it throws a weird outofrange error when trying to get the prefabs from spawnprefabs[]. Have checked that I added the relevant prefabs in the spawnable section, so kinda at a lost now :/ hope someone can help!
     
unityunity