In Unity/UNet: How do you properly spawn a `NetworkPlayer`?
In Unity/UNet:
How do you properly spawn a NetworkPlayer
? Right now, I'm doing it like this from inside a NetworkManager
derived class:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
NetworkPlayer newPlayer = Instantiate<NetworkPlayer>(m_NetworkPlayerPrefab);
DontDestroyOnLoad(newPlayer);
NetworkServer.AddPlayerForConnection(conn, newPlayer.gameObject, playerControllerId);
}
This code snippet works pretty well and both clients can communicate with each other. However, there are a few little issues that arise only on the host:
- In Unity's hierarchy-view on the host, there are only two
NetworkPlayer
instances. Shouldn't there be four NetworkPlayer instances on the host? Two client instances and two server instances? If so, do you have any ideas what could cause the missingNetworkPlayer
instances? - The two NetworkPlayer instances have both, their
isClient
andisServer
flags set totrue
. But only one of the has it'sisLocalPlayer
flag set. Now I wonder if this behavior is as intended? And if so, how do you distinguish between the client and the server instance of aNetworkPlayer
? - Two player behavior: If the remote client sends a
[Command]
that changes a[SyncVar]
on the server, then on the host, the[SyncVar]-hook
is called only on theNetworkPlayer
instance that represents the remoteNetworkPlayer
. The[SyncVar]-hook
is not called on the host's"isLocalPlayer-NetworkPlayer"
instance. Shouldn't the[SyncVar]-hook
be called on bothNetworkPlayer
instances?
Any advise is welcome. Thank you!