4
\$\begingroup\$

According to the "High Level API" documentation, unless I misread something, a Host "just" runs both as a Server and as a Client, so much that it would require no special support, i.e. it should run out of the box.

When the Server and Client are in separate processes, both work correctly. When a host acts as both the server and client, this is not the case.

When OnServerReady gets called on the Host, the NetworkConnection conn parameter has .hostId==-1, and if I try to call on NetworkServer.AddPlayerForConnection I'll get a warning and an error.

ClientScene::InternalAddPlayer: playerControllerId higher than expected: 0
UnityEngine.Networking.NetworkServer:AddPlayerForConnection(NetworkConnection, GameObject, Int16)
[...]

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.ClientScene.InternalAddPlayer (UnityEngine.Networking.NetworkIdentity view, Int16 playerControllerId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:90)
[...]

If I refrain from calling AddPlayerForConnection, then I'll encounter a warning (and no result) as soon as the Host calls a Command:

Trying to send command for non-local player.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
[...]

In fact, isLocalPlayer is false.

Am I doing something wrong, is the documentation wrong, or is this a bug?


As requested, some piece of code:

public override void OnServerReady (NetworkConnection conn)
{
    base.OnServerReady(conn);

    Transform spawnPoint = FindObjectOfType<SpawnPoints>().getNextSpawnLocation();

    GameObject controllerGO = instancedElements.SpawnObjectFree(playerPrefab,spawnPoint); // this is basically an Instantiate
    GameObject playerGO = instancedElements.SpawnObjectFree(playerCreaturePrefab,spawnPoint);
    [...]
    NetworkServer.AddPlayerForConnection(conn,controllerGO,0);
    NetworkServer.Spawn(playerGO);
    [...]
}

(I've trimmed it down to remove the non-relevant parts)

\$\endgroup\$
2
  • \$\begingroup\$ Can you post the code you are using to set up your Host and Client? \$\endgroup\$ Commented Jun 20, 2015 at 7:49
  • \$\begingroup\$ @JeremiahLeslie I've posted OnServerReady, the client does nothing special on setup, let me know if you need me to dig something else… \$\endgroup\$
    – o0'.
    Commented Jun 20, 2015 at 8:06

1 Answer 1

1
\$\begingroup\$

In the OnServerReady callback the connectionID is passed as -1 to indicate a no connection between server to server. In the connectionID parameter you should pass the connection between the localClient and server using NetworkServer.localConnections. If localConnections return only -1, then it means that you are not connected as localClient. Refer unity docs and look for the word host

\$\endgroup\$
3
  • \$\begingroup\$ On NetworkServer.localConnections I have one connection, which in turn still has .hostId==-1. \$\endgroup\$
    – o0'.
    Commented Jun 21, 2015 at 8:28
  • 1
    \$\begingroup\$ That means you are not connecting to the server as localClient something like this here docs.unity3d.com/Manual/UNetClientServer.html \$\endgroup\$ Commented Jun 21, 2015 at 17:19
  • \$\begingroup\$ @user3506498 this is the correct answer, i recommend you supply this as an answer instead of a comment so you can be voted for the bounty. \$\endgroup\$
    – Stephan
    Commented Jun 25, 2015 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .