- Home /
How to instanciate player prefab in a OnLobbyServerSceneLoadedForPlayer override ?
Hi everbody.
I'm in trouble with the UNET NetworkLobbyManager.
What I did :
In my lobby scene, the user can choose the character he want to play. So I have some variables in a custom lobbyPlayer for save this choice.
In the NetworkLobbyManager, the OnLobbyServerSceneLoadedForPlayer is overriden in order to instantiate the right prefab according to the data previously stored on the lobbyPlayer.
Here is my CustomNetworkLobbyManager class :
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CustomNetworkLobbyManager : NetworkLobbyManager
{
public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
{
//Retrieve the lobby player connection object
NetworkIdentity netId = lobbyPlayer.GetComponent<NetworkIdentity>();
NetworkConnection conn = netId.connectionToClient;
//Instantiate de right prefab from the spawn prefabs list according to the lobbyPlayer data
CustomNetworkLobbyPlayer customLobbyPlayer = lobbyPlayer.GetComponent<CustomNetworkLobbyPlayer>();
GameObject playerPrefab = spawnPrefabs[customLobbyPlayer.GetPlayerClass()];
GameObject newGamePlayer =
(GameObject) GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
//Init player data from the lobbyPlayer data
PlayerData playerData = newGamePlayer.GetComponent<PlayerData>();
playerData.SetPlayerTeam(customLobbyPlayer.GetPlayerTeam());
playerData.SetPlayerClass(customLobbyPlayer.GetPlayerClass());
//Destroy old player object
Destroy(gamePlayer.gameObject);
//Switch control to new player object
NetworkServer.ReplacePlayerForConnection(conn, newGamePlayer, 0);
return true;
}
}
On the editor side the NetworkLobbyManager is setup like this :
I have no dedicated server but a Host.
What I get
Whene I launch the game and select characters on both the client and the host, the game scene is loaded and the folowing Warning appears 4 times :
Trying to send command for non-local player.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
PlayerData:CallCmdTransmitPlayerTeam(Int32)
PlayerData:SetPlayerTeam(Int32) (at Assets/Scripts/PlayerBehaviour/PlayerData.cs:38)
CustomNetworkLobbyManager:OnLobbyServerSceneLoadedForPlayer(GameObject, GameObject) (at Assets/Scripts/Network/CustomNetworkLobbyManager.cs:22)
UnityEngine.Networking.NetworkLobbyPlayer:OnLevelWasLoaded()
When I move the character on host-side, everything works fine.
But when I do so on client-side, the player position is not updated on host-side and the following Warning is thrown each time a sync packet is sent accross the network :
Player unetview deleted when handling Command message [playerControllerId=0]
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
More Precisions (maybe no correlation)
1) If I set the playerControllerId to 1 istead of 0 on the NetworkServer.ReplacePlayerForConnection() call (line 29 of the given script), I get the 4 usual "Trying to send command for non-local player" Warnings, but a 5th is thrown :
ClientScene::InternalAddPlayer: playerControllerId higher than expected: 1
UnityEngine.Networking.NetworkServer:ReplacePlayerForConnection(NetworkConnection, GameObject, Int16)
CustomNetworkLobbyManager:OnLobbyServerSceneLoadedForPlayer(GameObject, GameObject) (at Assets/Scripts/Network/CustomNetworkLobbyManager.cs:29)
UnityEngine.Networking.NetworkLobbyPlayer:OnLevelWasLoaded()
But this time, the player positions are proprely updated on both host and client.
2) If I launch the host via the .exe (not inside the uity editor), the game crash on selecting character with the following error :
NetworkLobbyManager OnClientReadyToBegin no player at lobby slot 1
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
In advance, thanks, and sorry for my english. I'll improve.
Hi @Cryoblast did you manage to achieve what you were trying to do ?
As far as I remember, the probleme disapeared when I upgraded to a later version of Unity.
Your answer
Follow this Question
Related Questions
How to filter Lobby filled matches(with max players) in UNET ListMatches()? 0 Answers
Change NetworkMatch's name, attributes, and password after creation 1 Answer
ListMatchResponse Matches Count is 0 on remote PC?!? Why is Matchmaking broken? 0 Answers
[Multiplayer Lobby] Spawned enemies not seen in client's space.... 1 Answer