I’m trying to make different prefabs spawn depending on which character class the player chooses, which also involves spawning characters after the client scene is loaded (rather than as soon as the scene loads). In the script reference page for NetworkManager.OnServerAddPlayer, it says ‘The default implementation for this function creates a new player object from the playerPrefab’, but OnServerAddPlayer isn’t a virtual function, so I can’t override it.
Is there a way to get around this and change the code to spawn a prefab from a set of options instead of using the default prefab defined in the NetworkManager object? Do I need to recreate the entire chain of events (listed in the ‘Player Objects’ section here) from scratch?
Put more straightforwardly, how do I spawn different characters?
Having a brief look at the docs, could your PlayerPrefab not be an empty object that can be filled with the required character?
It could contain the scripts needed for player input and the like, but the class and it’s specific components could be either spawned and parented afterward, or, if that’s not possible (I did say a brief look), the object could contain all the classes and relevant scripts, disabled, and you can enable the appropriate one as needed.
I haven’t dealt with this area of things a lot, so I apologize if everything I just said is nonsense.
public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}