- Home /
When Networking do you need to use your IP address?
Just as the title says. I know the answer may seem obvious or that, yes, everyone has to use their IP address, but I followed a certain tutorial. Everything has worked well so far, but when I put in my IP address for the ipAddress variable, and try to connect or start a server on the game, it freezes or says that the server cannot connect.
Here is the code:
var ipAddress : String = "127.0.0.1";
var port : int = 25167;
var maxConnections : int = 10;
var playerGO : GameObject;
var log : String = "";
var myMessage : String = "";
var chatting : boolean = false;
function OnGUI () {
if (Network.peerType == NetworkPeerType.Disconnected) {
GUILayout.BeginHorizontal ();
ipAddress = GUILayout.TextField (ipAddress);
GUILayout.Label ("IP ADDRESS");
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
var tempPort : String ;
tempPort = GUILayout.TextField (port.ToString());
port = parseInt(tempPort);
GUILayout.Label ("PORT");
GUILayout.EndHorizontal ();
if (GUILayout.Button ("CONNECT")) {
for (var l : GameObject in FindObjectsOfType (GameObject)) {
l.BroadcastMessage ("OnLoadNetworkLevel", SendMessageOptions.DontRequireReceiver);
}
print ("Connecting . . .");
Network.Connect (ipAddress, port);
}
if (GUILayout.Button ("START SERVER")) {
for (var l : GameObject in FindObjectsOfType (GameObject)) {
l.BroadcastMessage ("OnLoadNetworkLevel", SendMessageOptions.DontRequireReceiver);
}
print ("Starting Server . . .");
Network.InitializeServer (maxConnections, port);
}
}
else {
if (GUILayout.Button ("DISCONNECT")) {
Network.Disconnect (200);
}
if (Input.GetKeyDown (KeyCode.T)) {
chatting = true;
}
if (Input.GetKeyDown (KeyCode.Escape)) {
chatting = false;
}
if (chatting) {
GUILayout.Label (log);
myMessage = GUILayout.TextField (myMessage);
if (GUILayout.Button ("SEND MESSAGE")) {
networkView.RPC ("Chat", RPCMode.All, myMessage);
}
}
}
}
function OnConnectedToServer () {
print ("Connected!");
OnLoadNetworkLevel ();
}
function OnPlayerDisconnected (player : NetworkPlayer) {
Network.RemoveRPCs (player);
Network.DestroyPlayerObjects (player);
}
function OnDisconnectedFromServer () {
Application.LoadLevel (Application.loadedLevel);
}
function OnLoadNetworkLevel () {
yield 25;
GetComponent (Camera).rect = Rect (0.7, 0.7, 0.25, 0.25);
GetComponent (GUILayer).enabled = false;
if (Network.peerType != NetworkPeerType.Disconnected) {
Network.Instantiate (playerGO, transform.position, transform.rotation, 0);
}
}
@RPC
function Chat (input : String) {
log += "\n";
log += input;
}
So do I need to use MY IP address? Or is there a more simple answer? Since currently my IP address isn't working for the networking.
It can't be just the IP address that's giving you a problem. Connecting to 127.0.0.1 will always connect as long as there are no program$$anonymous$$g errors, even if ports aren't open. $$anonymous$$ake sure you don't have any other mistakes that could cause it to not go through.
I think I've found my problem. The server works, and I found out why I couldn't connect. I can start the server, chat, and everything, but the screen is blank. I see no map that I've created, my character, nothing.