1

I want to determine if the newly connected player is a client and on server player(host) remove a msg. In UNET i want there to be two players over LAN. The first player is the server player and then their opponent connects.

The player searches for a LAN match to join like so:

public void StartPlayerClient() {
    Debug.Log ("client set up");
    discovery.Initialize ();
    discovery.StartAsClient ();
    searching = true;
}

Once a match is found it receives displays it like this:

void Update() {
    if (searching) {
        if (discovery.broadcastsReceived != null) {
            foreach (var addr in discovery.broadcastsReceived.Keys) {
                var value = discovery.broadcastsReceived [addr];
                //instantiate ui button to connect...
                GameObject ui = Instantiate (GameController.instance.multiplayerMatchConnect) as GameObject;
                ui.GetComponent<ConnectToMatch> ().Initialize (value);
                ui.transform.SetParent (GameController.instance.matchListParent.transform, false);
            }

            if (discovery.broadcastsReceived.Keys.Count > 0) {
                searching = false;
            }
        } else {
            searching = false;
        }
    }
}

then the player is able to connect by clicking the button that was just instantiated like so:

public void Initialize(NetworkBroadcastResult value) {
    res = value;
    GetComponent<Button> ().onClick.AddListener (Connect);

}

void Connect() {
    Debug.Log ("connecting...");
    string dataString = BytesToString (res.broadcastData);
    var items = dataString.Split(':');


    if (items.Length == 3 && items[0] == "NetworkManager") {
        if (NetworkManager.singleton != null && NetworkManager.singleton.client == null) {
            NetworkManager.singleton.networkAddress = items[1];
            NetworkManager.singleton.networkPort = Convert.ToInt32(items[2]);
            NetworkManager.singleton.StartClient();
        }

    }
}

Now that all works fine, these are the troublesome parts:

//called on the server when client connects
public override void OnServerConnect(NetworkConnection conn) {
    if (NetworkServer.connections.Count > 1) {
        GameController.instance.pnlWaitingForOtherPlayer.SetActive (false);
        Debug.Log ("player connected");
        GameController.instance.pnlMatchList.SetActive (false);
        GameController.instance.pnlJoinCreateMenu.SetActive (false);
    }
}

on the server client it will say "Waiting for other player" before they connect.. when they do i want the msg to disappear for the server player, this is how im trying to do it and it does not work.

//called on the client when connects to the server
public override void OnClientConnect(NetworkConnection conn) {
    if (!discovery.isServer) {
        GameController.instance.pnlMatchList.SetActive (false);
        Debug.Log("you connected");
    }
}
  • Does the message `"Waiting for other player" ` appear on the screen or in the console? – Hristo May 11 '17 at 11:37
  • @Hristo the msg is in the UI panel (so on the screen) called pnlWaitingForOtherPlayer.. i set it to false when the second player connects in OnServerConnect but thats the problem. its not occurring because NetworkServer.connections.Count doesnt equal 2 when the second player connects... ive also tried commenting out the > 1 condition and it OnServerConnect just doesnt get called.. :( –  May 11 '17 at 12:07
  • Could you provide the code where you call the method `OnServerConnect`? – Hristo May 11 '17 at 12:10
  • its an override method. OnServerConnect is a result of the Connect() method's StartClient() call cos OnServerConnect is called on the server when a player joins –  May 11 '17 at 12:12
  • Could you make sure the method even gets called? like adding a `debug.log` or something? – Hristo May 11 '17 at 12:30
  • @Hristo if i remove the condition around it, it does work but only on the server. when the client connects it does not get called on the server –  May 11 '17 at 12:34

0 Answers0