Unity Networking, connecting to sever problems :(

Hey :):slight_smile: So ive been following a tutorial and have this code as working progress for networking and when i build the game and have run in background on, nothing happens. On one game it says server initialized and then when i try to join the game on the other it wont allow me to. any ideas of what im doing wrong?

var gameName: String ="Atomizer_Networking_Curtis";

private var refreshing: boolean;
private var hostData: HostData[];

private var btnX: float;
private var btnY: float;
private var btnW: float;
private var btnH: float;

function Start(){
	btnX = Screen.width * 0.1;
	btnY = Screen.width * 0.1; 
	btnW = Screen.width * 0.1; 
	btnH = Screen.width * 0.1;
} 

function startServer(){
	Network.InitializeServer(4, 25001, !Network.HavePublicAddress);
	MasterServer.RegisterHost(gameName,"Tutorial Game Name","This is a tutorial game"); 
}

function refreshHostList(){
	MasterServer.RequestHostList(gameName);
	refreshing = true;

}


function Update(){
	if(refreshing){
		if(MasterServer.PollHostList().Length > 0){
			refreshing = false;
			Debug.Log(MasterServer.PollHostList().Length);
			hostData = MasterServer.PollHostList();
		}
	}
}


// Messages

function OnServerInitialized(){
	Debug.Log("Server Initialized!");
}

function OnMasterServerEvent(mse:MasterServerEvent){
	if(mse == MasterServerEvent.RegistrationSucceeded){
		Debug.Log("Registered Server!");
	}
}

//GUI

function OnGUI(){
	if(!Network.isClient && !Network.isServer){
		if(GUI.Button(Rect(btnX, btnY, btnW, btnH), "Start")){
			Debug.Log("Starting Server");
			startServer();
		}
	
		if(GUI.Button(Rect(btnX, btnY * 1.2 + btnH, btnW, btnH), "Refresh")){
			Debug.Log("Refreshing");
			refreshHostList();
		}	
		
		if(hostData){
			for(var i : int = 0; i < hostData.length; i ++ ){
				if(GUI.Button(Rect(btnX * 1.5 + btnW, btnY * 1.2 + (btnH * i), btnW * 3, btnH * 0.5), hostData*.gameName));*

_ Network.Connect(hostData*);_
_
}_
_
}_
_
}_
_
}*_

Please format your code next time. It is done by selecting your code and pressing the "10101" button. I have done it for you now, but I would like to stress the importance of doing so the next time.

"It won't allow me to". There are a lot of things that could mean. What exactly happens when you try to join?

1 Answer

1

http://forum.unity3d.com/threads/126170-Failed-ot-connect-because-this-system-is-already-connected

Your problem is most likely on line 69 in your code:

 if(GUI.Button(Rect(btnX * 1.5 + btnW, btnY * 1.2 + (btnH * i), btnW * 3, btnH * 0.5), hostData*.gameName));*

You have a semicolon at the end of the if statement, which means that Network.Connect is being called multiple times.
Remove the semicolon from the end of that line.

If it solved the error then please mark the answer as accepted by clicking the green arrow. Thank you.