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");
}
}