Network.PeerType is never Connecting

I would like to rely on the network.peertype variable to control whether to show the game selecting UI. If the UI goes unmanaged, the user might be able to connect to multiple games or cause all kinds of trouble for themselves.

Expected:

  • As soon as I call Network.Connect, Network.peerType is set to NetworkPeerType.Connecting until the connection succeeds or fails

Actual:

  • Network.peerType remains NetworkPeerType.Disconected until the connection succeeds or fails

Code:

	void OnGUI () {
		if (Network.peerType == NetworkPeerType.Disconnected) {
			GUILayout.BeginHorizontal();
			if(GUILayout.Button("Start New Game")) {
				Network.InitializeServer(1, port, !Network.HavePublicAddress());
				MasterServer.RegisterHost(gameType, gameName);
				Application.LoadLevel("footballBoard");
			}
			gameName = GUILayout.TextField(gameName);
			GUILayout.EndHorizontal();
			HostData[] data  = MasterServer.PollHostList();
			// Go through all the hosts in the host list
			foreach (var element in data)
			{
				GUILayout.BeginHorizontal();	
				var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
				GUILayout.Label(name);	
				GUILayout.Space(5);
				string hostInfo = "[";
				foreach (var host in element.ip) {
					hostInfo = hostInfo + host + ":" + element.port + " ";
				}
				hostInfo = hostInfo + "]";
				GUILayout.Label(hostInfo);	
				GUILayout.Space(5);
				GUILayout.Label(element.comment);
				GUILayout.Space(5);
				GUILayout.FlexibleSpace();
				if (GUILayout.Button("Connect"))
				{
					clicks++;
					// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
					Network.Connect(element);
				}
				GUILayout.EndHorizontal();	
			}
		}
	}

I can manage the state myself, but why doesn’t this work?

Sorry, it’s late to answer this, but google shows this link in first place when I search for “Network.peerType”. For those people who came here looking for answer about this problem, it isn’t a unity bug, just set “Run In Background = true” at Player Settings and “Network.peerType ==NetworkPeerType.Disconnected” is work fine.

Para quem está lendo em português, se não setar “Run In Background = true” em Edit/Project Settings/Player e fizer testes locais em sua máquina conectando duas instâncias do jogo, a que tem o server estará em background quando o cliente conectar, e Network.peerType continuará == NetworkPeerType.Disconnected e os exemplos encontrados na net não funcionarão.

Espero ter ajudado.