How to spawn a different game player prefab based on player choice with Network Lobby Manager?
Hi, I'm trying to to spawn a different game player prefab based on player choice with the Network Lobby Manager asset downloadable from the asset store (asset store link). To do so I'm using code on 3 different scripts:
GameManager = offline game object that handles all the offline menus, included the selection menu from which the player chooses which character he wants to play with. Once the player is selected it spawns the LobbyManager prefab.
LobbyPlayer = modified the code of the original adding an Update function that searches the selected game player prefab inside the GameManager.
LobbyManager = modified to get the player prefab from the lobby player and spawning it instead of the default one.
I've already added all the possible game player prefabs to the spawnable prefabs in the LobbyManager inspector and given the GameManager game object to the LobbyPlayer inspector directly.
The GameManager has no network identity and simply stores the selected prefab in a public GameObject variable (chosenPC). I don't think the code of the GameManager is needed to evaluate the problem so I will omitt it unless requested.
Here's the code I added to the LobbyPlayer:
public GameObject PlayChar = null;
public GameObject ScreenMan; //GameManager
void Update()
{
if (PlayChar == null && ScreenMan.GetComponent<manageGame>().chosenPC != null)
{
PlayChar = ScreenMan.GetComponent<manageGame>().chosenPC;
Debug.Log(PlayChar.name);
}
}
And here's the code I added in the LobbyManager:
public Dictionary<int, GameObject> currentPlayers = new Dictionary<int, GameObject>();
public Dictionary<int, LobbyPlayer> currentLobbyPlayers = new Dictionary<int, LobbyPlayer>();
public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
{
//original script
GameObject obj = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
//original script
LobbyPlayer newPlayer = obj.GetComponent<LobbyPlayer>();
//original script
newPlayer.ToggleJoinButton(numPlayers + 1 >= minPlayers);
//my added code
if (!currentLobbyPlayers.ContainsKey(conn.connectionId))
{ /*takes connectionsID into the Dictionary and the LobbyPlayer
to have correspondence between the 2 and be able to distinguish which player has selected which character*/
currentLobbyPlayers.Add(conn.connectionId, newPlayer);
}
.... //original code goes on
}
//this code was all added by me
public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
{
LobbyPlayer newPlayer = currentLobbyPlayers[conn.connectionId];
GameObject temp = null;
/*I probably don't need this second dictionary but before this try the code was in a different function therefore I needed it and for simplicity I added as it was, I'm pretty sure this is not a problem thou*/
if (!currentPlayers.ContainsKey(conn.connectionId))
{ //takes connectionsID into the Dictionary and the chosen PC from LobbyPlayer
temp = newPlayer.PlayChar;
if (temp != null)
{
currentPlayers.Add(conn.connectionId, temp);
}
Debug.Log(temp.name);
}
GameObject actualPC = currentPlayers[conn.connectionId];
Vector3 startpos = new Vector3(0.0f, 0.0f, 0.0f);
GameObject _temp = (GameObject)GameObject.Instantiate(actualPC, startpos, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, _temp, playerControllerId);
return _temp;
}
This game uses the client/host network modality, indipendently of the fact that the unity execution is the host or the client the LobbyPlayer prefabs don't have the PlayChar variable defined in the inspector (once instantiated of course). So, to correct the scope of my question, how can I pass this information to the LobbyPlayer so that the LobbyManager is able to access it?
I've started using Unity only a couple months ago therefore I'm sorry if my code isn't very good or I've missed something simple, the fact is that I've been banging my head on this problem for 3 whole days and I've tried any approach I could come up with. Any kind of help will be greatly appreciated, thanks!
Your answer
Follow this Question
Related Questions
Unity Lobby, player get disconnected if game has already started. 1 Answer
OnLobbyServerSceneLoadedForPlayer not called for client 1 Answer
UNet - Is there a way to get clients and their assigned gameobjects? 0 Answers
How to count score on all players from non player object 0 Answers
How do I get the true position (Vector 3) from a Network Transform Component(Unet)? 0 Answers