- Home /
networking going all wrong
I am using a network script that i think should work (and it does if i use 2 instances on my pc). But my friend opened the server (on a port that worked with another project) and it didn't work, i simly got the message that it couldn't connect to the server.
i don't understand this at all but this is my script.
#pragma strict
var mySkin : GUISkin;
var state = 1;
var IP : String = "ip adress";
var port : int = 20005;
var connected : boolean = false;
private var name1 : String = "unit1";
private var name2 : String = "unit2";
private var name3 : String = "unit3";
private var name4 : String = "unit4";
private var name5 : String = "unit5";
function Start ()
{
Time.timeScale = 1;
name1 = PlayerPrefs.GetString("Name1");
name2 = PlayerPrefs.GetString("Name2");
name3 = PlayerPrefs.GetString("Name3");
name4 = PlayerPrefs.GetString("Name4");
name5 = PlayerPrefs.GetString("Name5");
}
function Update ()
{
if(Network.peerType == NetworkPeerType.Client || Network.peerType == NetworkPeerType.Server)
{
connected = true;
}
if(Network.connections.Length > 0)
{
Application.LoadLevel("1");
}
}
function OnGUI ()
{
GUI.skin = mySkin;
if(state == 1)
{
GUI.Box(new Rect(Screen.width/2-150, 0, 300, Screen.height), "Menu");
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2-200,300,30), "Play"))
{
state = 2;
}
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2,300,30), "Site"))
{
UnityEngine.Application.OpenURL("http://eyeongames.weebly.com/");
}
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,30), "Quit"))
{
Application.Quit();
}
if(GUI.Button(new Rect(Screen.width/2+500, Screen.height/2+300, 70, 40), "Crew"))
{
state = 3;
}
}
else
if(state == 2)
{
GUI.Box(new Rect(Screen.width/2-150, 0, 300, Screen.height), "Menu");
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2-200,300,30), "Host"))
{
state = 4;
}
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2,300,30), "Join"))
{
state = 5;
}
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,30), "Back"))
{
state = 1;
}
if(GUI.Button(new Rect(Screen.width/2+500, Screen.height/2+300, 70, 40), "Crew"))
{
state = 3;
}
}
else
if(state == 3)
{
GUI.Box(new Rect(Screen.width/2-100,Screen.height/2-125,200,250), "Crew");
if(GUI.Button(new Rect(Screen.width/2+500, Screen.height/2+300, 70, 40), "Back"))
{
state = 1;
PlayerUnitNames.Save(name1, name2, name3 ,name4, name5);
}
name1 = GUI.TextField(new Rect(Screen.width/2-100, Screen.height/2-70, 200, 25), name1);
name2 = GUI.TextField(new Rect(Screen.width/2-100, Screen.height/2-40, 200, 25), name2);
name3 = GUI.TextField(new Rect(Screen.width/2-100, Screen.height/2-10, 200, 25), name3);
name4 = GUI.TextField(new Rect(Screen.width/2-100, Screen.height/2+20, 200, 25), name4);
name5 = GUI.TextField(new Rect(Screen.width/2-100, Screen.height/2+50, 200, 25), name5);
}
else
if(state == 4)
{
GUI.Box(new Rect(Screen.width/2-150, 0, 300, Screen.height), "Host");
port = int.Parse(GUI.TextField(new Rect(Screen.width/2-150,Screen.height/2,300,30), port.ToString()));
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,30), "Go"))
{
state = 6;
Network.InitializeServer(32, port, false);
}
}
else
if(state == 5)
{
GUI.Box(new Rect(Screen.width/2-150, 0, 300, Screen.height), "Join");
IP = GUI.TextField(new Rect(Screen.width/2-150,Screen.height/2-200,300,30), IP);
port = int.Parse(GUI.TextField(new Rect(Screen.width/2-150,Screen.height/2,300,30), port.ToString()));
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,30), "Go"))
{
state = 6;
Network.Connect(IP, port);
}
}
else
if(state == 6)
{
GUI.Box(new Rect(Screen.width/2-150, 0, 300, Screen.height), "Waiting for other player");
if(GUI.Button(new Rect(Screen.width/2-150,Screen.height/2+200,300,30), "Back"))
{
state = 1;
}
}
}
Answer by orb · Sep 14, 2014 at 08:07 PM
Most people are behind NAT on their modem-routers, so ports need to be explicitly opened somehow. Have your friend check if the ports are connectable while running the server. This site lets you verify accessibility of ports. If it can't connect, you need to change port forwarding or port triggers in the router.
Answer by LucasVmarrewyk · Sep 14, 2014 at 09:45 PM
Thanks for answering. the weird thing is everything works fine on an old project (using same ip and port).
Your answer
