Setting the playerPrefab of network manager dynamically
When I set the playerPrefab variable of my NetworkManager dynamically and click Start Host, the playerPrefab gets wiped and instead gets set to None, resulting in an error. How do I get a dynamic playerPrefab to persist through scene changes on a NetworkManager?
GameObject player = new GameObject();
// ... dynamically build player
// Assign playerPrefab to network manager
NetworkManager nm = GetComponent<NetworkManager>();
nm.playerPrefab = player;
Answer by christopf · Nov 19, 2016 at 06:03 PM
i think you have to register your prefab first in the OnClientConnect() function of your NetworkManager class, like creating a script that inherits from your networkManager and then
public override void OnClientConnect(NetworkConnection conn) {
ClientScene.RegisterPrefab(player); // player is your gameObject from your example
base.OnClientConnect(conn);
}
not sure if you were asking for that?
When you want to add the prefab to the scene wpuld be the next question to ask. if you want the prefab loaded when the actual player joins, you can use:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
Your answer
Follow this Question
Related Questions
Unity network manager assign playerprefab at runtime 0 Answers
how to spawn a player prefab on a server 0 Answers
Help with cameras, player prefab on network 0 Answers
How can I attach a Gameobject from the scene to a prefab on the Assets folder? 0 Answers
Unet - whats the best approach for a global game manager? 0 Answers