- Home /
Why won't my network code run on LAN?
Here's my network code, I can connect on the same machine but when I try two separate computers it won't connect.
var ServerIP: String = "127.0.0.1";
var ServerPort: String = "30000";
function OnGUI(){
if (Network.peerType == NetworkPeerType.Disconnected){
if (GUILayout.Button("Connect")){
Network.Connect(ServerIP, parseInt(ServerPort));
}
if (GUILayout.Button("New Server")){
Network.InitializeServer(32, parseInt(ServerPort), false);
}
}else{
if (GUILayout.Button("Disconnect")){
Network.Disconnect();
}
}
}
Answer by PProductions · Jul 08, 2013 at 05:06 PM
The ip address 127.0.0.1 is connecting to the same machine, to connect to a different machine you need that machine's ip address. If the other computer is on your internet network(e.g. connected to your wifi) then you can connect using it's local ip. If it is not then you will need to port forward the global ip through your firewall so you can host a server on it. I would suggest looking up some youtube videos on minecraft server setups as I find they help explain port forwarding.
However, if I were you I would be using unity's master server functionality. It is much more advanced and in this example you don't need to worry about knowing the server's ip directly.
Some useful links:
http://www.youtube.com/user/steamisM50/search?query=multiplayer
http://docs.unity3d.com/Documentation/ScriptReference/MasterServer.html
I hope this helps
Answer by mor0022 · Apr 15, 2014 at 06:57 AM
Here is my Code, Still trying to work out how to destroy objects neatly.
using UnityEngine;
using System.Collections;
public class LanNetworkManager : MonoBehaviour {
public string gameName = "not needed for lan";
public string hostGameName = "Tutorial Networking";
public GameObject playerPrefab;
public Transform spawnObject;
public int maxPlayers = 6;
private string ip = "127.0.0.1";
private int playerCount = 0;
private bool gameStarted = false;
private int btnX;
private int btnY;
private int btnW;
private int btnH;
void Start(){
btnX = 10;
btnY = 10;
btnW = 130;
btnH = 20;
}
void startServer(){
bool useNat = false; //!Network.HavePublicAddress ();
Network.InitializeServer (maxPlayers, 25017,useNat);
}
void OnServerInitialized(){
Debug.Log ("Server Started");
spawnPlayer ();
}
void OnConnectedToServer(){
Debug.Log ("Server Joined");
spawnPlayer ();
}
void ConnectToLanServer(string ipA){
Network.Connect (ipA, 25017);
}
void OnPlayerConnected(){
playerCount++;
Debug.Log ("Player Connected");
}
void OnPlayerDiconnected(){
playerCount--;
Debug.Log ("Player Diconnected");
}
void OnGUI(){
if (!Network.isClient && !Network.isServer && !gameStarted) {
if (GUI.Button (new Rect (btnX, btnY, btnW, btnH), "Start Server")) {
gameStarted = true;
startServer ();
}
ip = GUI.TextField (new Rect (btnX, btnY * 4, btnW, btnH), "127.0.0.1");
if (GUI.Button (new Rect (btnX, btnY * 7, btnW, btnH), "Manual Connect")) {
gameStarted = true;
ConnectToLanServer (ip);
}
} else {
if(GUI.Button (new Rect(btnX,btnY*11,btnW,btnH),"Disconnect")){
gameStarted = false;
//Network.DestroyPlayerObjects(Network.player);
Network.Disconnect(500);
}
}
if (Network.isServer) {
GUI.Label (new Rect(btnX,btnY*15,btnW,btnH),"Players: " + (playerCount + 1) + "/" + maxPlayers);
}
}
void PlayerSpawned(){
Debug.Log ("Player Spawned");
}
void spawnPlayer(){
Network.Instantiate (playerPrefab, spawnObject.position, Quaternion.identity, 0);
PlayerSpawned ();
}
}