UNet My Host Script Spawns 2 Copies of the Player Object

Hi All,

Hoping someone can help with this scenario. I have created my own NetServer & NetClient scripts so that I do not have to use the NetworkManager provided by Unet.

All is going well - NetServer works & NetClient works and then I want to combine them to create a Host (Server + Client). The problem I face is that when I run the Host it generates a server player and a client player on the local machine simultaneously when I only need one Player object.

The spawn code resides in NetServer in the OnAddPlayer Callback:

var playerGo = Instantiate<GameObject>(globals.playerKnightPrefab);
NetworkServer.AddPlayerForConnection(netMsg.conn, playerGo, 0);

From what I have read in the Unity Docs the NetworkServer.AddPlayerForConnection call will create a synced object on the clients (does not require NetworkServer.Spawn). What I need is a way to specify that this is a HOST scenario and that only one object is needed for the local machine.

Can anyone point me in the right direction to stop the double spawn (Server spawns object and then Client spawn the same object)?

Thanks in advance.

Thanks @DiegoSLTS. It’s funny I was just doing that exact thing ! I’m still trying to get my head around what Unet is doing with NetworkManager. The part that relates to my question seems to be:

void OnServerAddPlayerInternal(NetworkConnection conn, short playerControllerId) {
    GameObject player;
    Transform startPos = GetStartPosition();
    if (startPos != null)
          player = (GameObject)Instantiate(m_PlayerPrefab, startPos.position, startPos.rotation);
           player = (GameObject)Instantiate(m_PlayerPrefab, Vector3.zero, Quaternion.identity);
    NetworkServer.AddPlayerForConnection(conn, player, playerControllerId); 

Yet this appears to be similar to what I am doing…hmmm. I’m missing something I guess.
Thanks for the tip though. Great minds and all that :slight_smile: