Search Unity

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

Discussion in 'Multiplayer' started by Nitrousek, Mar 27, 2017.

  1. Nitrousek

    Nitrousek

    Joined:
    Jan 31, 2016
    Posts:
    38
    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.

    Code (CSharp):
    1.     public void OnConnected(NetworkMessage msg)
    2.     {
    3.         Debug.Log("Connected!");
    4.         // below added by me
    5.         Debug.Log(NetworkServer.active);
    6.         FindObjectOfType<SpawnManager>().CmdSpawn(playerPrefab);
    7.     }
    Code (CSharp):
    1. public class SpawnManager : NetworkBehaviour {
    2.  
    3.     [ClientRpc]
    4.     public void RpcSpawn(GameObject go)
    5.     {
    6.         GameObject thePlayer = Instantiate(go, Vector3.zero, Quaternion.identity);
    7.         NetworkServer.AddPlayerForConnection(connectionToClient, thePlayer, 0);
    8.         Debug.Log("Spawned successfully");
    9.     }
    10.  
    11.     [Command]
    12.     public void CmdSpawn(GameObject go)
    13.     {
    14.         RpcSpawn(go);
    15.     }
    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:
    Code (CSharp):
    1.   FindObjectOfType<SpawnManager>().CmdSpawn(playerPrefab);
    because the object is inactive (it has networkidentity)

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. 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.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    Last edited: Mar 28, 2017
    Nitrousek likes this.
  3. Nitrousek

    Nitrousek

    Joined:
    Jan 31, 2016
    Posts:
    38
    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 :) 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.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not sure you can pass prefabs though in the same way you can pass a GameObject that is alive in the scene.
     
  5. Nitrousek

    Nitrousek

    Joined:
    Jan 31, 2016
    Posts:
    38
    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.