setting up server-client communication according to unity tutorial not working

https://docs.unity3d.com/Manual/UnityMultiplayerIntegratingHighLevel.html

I have copy&pasted it, tried ‘creating the room’

then joined the room.

Everything seems to be working fine, at least according to debug messages.
Then, I tried spawning my player via cmd/rpc, by adding a simple line of code into the tutorial-code.

    public void OnConnected(NetworkMessage msg)
    {
        Debug.Log("Connected!");
        // below added by me
        Debug.Log(NetworkServer.active);
        FindObjectOfType<SpawnManager>().CmdSpawn(playerPrefab);
    }
public class SpawnManager : NetworkBehaviour {

    [ClientRpc]
    public void RpcSpawn(GameObject go)
    {
        GameObject thePlayer = Instantiate(go, Vector3.zero, Quaternion.identity);
        NetworkServer.AddPlayerForConnection(connectionToClient, thePlayer, 0);
        Debug.Log("Spawned successfully");
    }

    [Command]
    public void CmdSpawn(GameObject go)
    {
        RpcSpawn(go);
    }

Now, the debug of “Networkserver.active” returns that the server is not active, even though i’m connected to it, and i’m inside the match. The spawn function fails, and I get nullpointer exception on line:

  FindObjectOfType<SpawnManager>().CmdSpawn(playerPrefab);

because the object is inactive (it has networkidentity)

NullReferenceException: Object reference not set to an instance of an object
HostGame.OnConnected (UnityEngine.Networking.NetworkMessage msg) (at Assets/HostGame.cs:101)

I am not sure what to do, the game was working fine when played via hud with combined host&player, but for this type of game I will need a dedicated server with players connecting to it. Any help would be appreciated.

Your instantiate and AddPlayerForConnection code needs to be run on the server, not the client. Commands are run on the server, ClientRpc’s are run on the client. The server is what needs to initially spawn your player object and associate it with your client’s connection.

Example:
https://docs.unity3d.com/ScriptReference/Networking.NetworkServer.AddPlayerForConnection.html

Also, I don’t believe you can just pass a gameobject in a command like that. Hopefully someone else chimes in on that one, but I thought you were limited to simple types.

1 Like

Ah yes, this was a mistake of mine, the networkserver spawn was supposed to go into command, thanks for that.

as for types:
The allowed argument types are;

• Basic type (byte, int, float, string, UInt64, etc)
• Built-in Unity math type (Vector3, Quaternion, etc),
• Arrays of basic types
• Structs containing allowable types
• NetworkIdentity
• NetworkInstanceId
• NetworkHash128
• GameObject with a NetworkIdentity component attached.

So I can pass the gameobject :slight_smile: probably not adviced though, because of size of the operation, but who cares, as long as it doesnt produce any errors in the long run.

I’m not sure you can pass prefabs though in the same way you can pass a GameObject that is alive in the scene.

Not sure about that. I had to resign from this idea all together, since I do not want to use unity’s matchmaking anyway, I want to host the servers myself.