- Home /
Unity Networking, connecting to sever problems :(
Hey :):) 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[i].gameName));
Network.Connect(hostData[i]);
}
}
}
}
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?
I play the build on one screen and run the server in the unity scene on the other (so running both) and go to join the server from the unity scene because im running the server off the build but it wont allow me to join any idea?
You can add the following functions to the bottom of your script to get more info on what is going wrong when you try to connect.
Then check your console for errors and post back the results or do a google search.
//Client functions called by Unity
function OnConnectedToServer() {
Debug.Log("This CLIENT has connected to a server");
}
function OnDisconnectedFromServer(info : NetworkDisconnection) {
Debug.Log("This SERVER OR CLIENT has disconnected from a server");
}
function OnFailedToConnect(error: NetworkConnectionError){
Debug.Log("Could not connect to server: "+ error);
}
//Server functions called by Unity
function OnPlayerConnected(player: NetworkPlayer) {
Debug.Log("Player connected from: " + player.ipAddress +":" + player.port);
}
function OnServerInitialized() {
Debug.Log("Server initialized and ready");
}
function OnPlayerDisconnected(player: NetworkPlayer) {
Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port);
}
function OnFailedToConnectTo$$anonymous$$asterServer(info: NetworkConnectionError){
Debug.Log("Could not connect to master server: "+ info);
}
function OnNetworkInstantiate (info : Network$$anonymous$$essageInfo) {
Debug.Log("New object instantiated by " + info.sender);
}
error says Failed to connect to 192.168.0.10:25001 because this system is already connected. This can occur when the network connection is disconnected too quickly for the remote system to receive the disconnect notification, for example when using Network.Disconnect(0).
comment says Could not connect to server: AlreadyConnectedToServer UnityEngine.Debug:Log(Object) Network$$anonymous$$anagerScript:OnFailedToConnect(NetworkConnectionError) (at Assets/Scripts/Network$$anonymous$$anagerScript.js:98)
Answer by ByteSheep · May 03, 2013 at 02:39 PM
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[i].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.
Thankyou heaps :) didnt even notice, guess lack of sleep is kicking in anyway thankyou
If it solved the error then please mark the answer as accepted by clicking the green arrow. Thank you.
if anyone else could, using the same code above, I have the characters spawning when you join the game but I want to have it so you can choose out of the two prefabs to use at the moment it just uses one, and i was wondering how you would do it so the player can choose
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
UDP implementation in Unity, unable to send to two different machines 0 Answers
Make code execute for a specified time? 1 Answer
How to rotate 90 degrees 4 Answers