Search Unity

[UNET] NullReferenceException OnServerAddPlayer and questions about sending messages

Discussion in 'UNet' started by Linearch, Jan 21, 2016.

  1. Linearch

    Linearch

    Joined:
    Apr 1, 2015
    Posts:
    33
    First of all, I have searched for these and I'm getting pretty desperate by now, haha.

    How do I send messages from client to server just like how server can do that with NetworkServer.Send?
    How do I reply to broadcast, or send message to a specific IP address?
    Why is the NetworkClient I get from MyNetworkManager.singleton.StartHost() or StartClient() null? (MyNetworkManager is derived from NetworkManager)

    Why do I get this error when calling ClientServer.AddPlayer(netId) and how to fix it?
    "Must call AddPlayer() with a connection the first time to become ready."

    Why do I get NullReferenceException when all my arguments aren't null?
    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Networking.ClientScene.InternalAddPlayer (UnityEngine.Networking.NetworkIdentity view, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:121)
    UnityEngine.Networking.LocalClient.AddLocalPlayer (UnityEngine.Networking.PlayerController localPlayer) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/LocalClient.cs:85)
    UnityEngine.Networking.NetworkServer.SetupLocalPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.Networking.NetworkIdentity uv, UnityEngine.Networking.PlayerController newPlayerController) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1025)
    UnityEngine.Networking.NetworkServer.InternalAddPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.GameObject playerGameObject, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:970)
    UnityEngine.Networking.NetworkServer.AddPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.GameObject player, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:934)
    MyNetworkManager.OnServerAddPlayer (UnityEngine.Networking.NetworkConnection conn, Int16 playerControllerId) (at Assets/Scripts/MyNetworkManager.cs:324)
    MyNetworkManager.SpawnChars () (at Assets/Scripts/MyNetworkManager.cs:311)
    MyNetworkManager.waitForScene () (at Assets/Scripts/MyNetworkManager.cs:297)
    MyNetworkBehaviour.FixedUpdate () (at Assets/Scripts/MyNetworkBehaviour.cs:17)
    public override void OnServerAddPlayer(NetworkConnection conn,short playerControllerId){
    ClientInfo info=clientInfos[conn.connectionId];
    Transform curSpawner=spawners.FindChild(info.team.ToString());
    GameObject plyr=(GameObject)Instantiate(Resources.Load("Char"+info.charId.ToString()),
    curSpawner.position+Vector3.ClampMagnitude(MyUtility.randomV3xz(0,100)/100f,2),
    curSpawner.rotation);
    plyr.GetComponent<PlyrCtrl>().team=info.team;
    plyr.GetComponent<ObjInfo>().team=info.team;
    plyr.GetComponent<PlyrCtrl>().myName=clientInfos[conn.connectionId].name;
    NetworkServer.AddPlayerForConnection(conn,plyr,playerControllerId); //this is line 324
    }

    I think that's all for now, thanks. I'm really hoping for a fast reply since I got a deadline.
     
    Last edited: Jan 21, 2016
  2. Linearch

    Linearch

    Joined:
    Apr 1, 2015
    Posts:
    33
    anyone pls?
     
  3. GunLengend

    GunLengend

    Joined:
    Sep 24, 2014
    Posts:
    54
    First of all, you code really hard to read.

    Then, at your error above : "Must call AddPlayer() with a connection the first time to become ready."
    It's mean client is not ready, you must call ClientScene.Ready()
    To Spawn player with AddPlayerForConnection, client must call ClientScene.RegisterPrefabs, and calling Scene.AddPlayer

    Then the Server will handle that message and run a function to execute AddPlayerForConnection
     
  4. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    To send a message from server to client register the function i find the best place to do this is in your networkmanager class.

    Code (CSharp):
    1.  
    2. public override void OnStartClient (NetworkClient client)
    3. {
    4. client.RegisterHandler(MsgType.Highest + 5,FunctionName);
    5. }
    6.  
    Then where functionname is, will be the function to call, i usually have this on the player object..

    Code (CSharp):
    1.   public void FunctionName(NetworkMessage netMsg) {}
    Then to send a message do something like this..

    Code (CSharp):
    1.   MessageClass msg = new MessageClass();
    2.         msg.messageToSend = "The message to send";
    3. NetworkServer.SendToClient(connectionid, MsgType.Highest + 5, msg);

    Hope that helps you.. ah i miss red what you said.. to send messages to server, you have to call a [Command] function, functions with [Command] above them get called on the server, so you can pass a string to the function and send it to the command for the server to recive.

    Never rely on ip addreses to identify a users on the server, because a person can be on a network of there own with lots of people using the same ip address, use the connectionid of the player connecting to the server, this id will be diferent for all people connected, and it needed for send messages and stuff, so make a list and when people connect add there connectids to it along with other information like player name etc
     
    Last edited: May 15, 2016