I’m using the default Connect function to do my networking, the server creation seems to work fine, but when i try to connect from another computer in the LAN or even the very host’s computer (using another window running the same build of the game) it does not connect to the host. I have used the same code in Unity 4.5/4.6 and it did work, even connect to the same computer, or even a Hamachi network.
I wasn’t able to test this with a computer outside my LAN, but even if i can connect that way, i want LAN connection to give players more options, like playing with a friend without having to pay for a dedicated server or joining a server with tons of people if they don’t want the social interaction. (like me when i play)
Also, i did try and open my modem’s ports, but nothing changed.
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public GameObject human;
string ip = "xxx.xxx.xxx.xxx";
string connectedPlayers;
void Update () { // this edits the string that shows in a side panel, for debugging
connectedPlayers = Network.player.ipAddress;
foreach (NetworkPlayer p in Network.connections) {
connectedPlayers = "
" + connectedPlayers + p.ipAddress;
}
}
void SpawnMyself () {
if (human != null) {
GameObject me = (GameObject)Network.Instantiate (human, Vector3.zero, Quaternion.identity, 0);
me.GetComponent<PlayerController> ().enabled = true;
me.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(4).GetChild(0).GetChild(0).GetChild(0).gameObject.SetActive(true);
} else {
Debug.LogError("No Human GameObject is set in " + transform.name);
}
}
void OnGUI () {
if (!(Network.isClient || Network.isServer)) {
ip = GUI.TextField (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f - 40, 200, 20), ip);
if (GUI.Button (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f - 10, 200, 20), "Connect")) {
ConnectToServer ();
}
if (GUI.Button (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f + 20, 200, 20), "Host server")) {
StartServer ();
}
GUI.TextArea (new Rect (0, 0, 200, Screen.height), connectedPlayers);
}
}
void ConnectToServer () {
Network.Connect(ip, 26000);
}
void StartServer () {
bool useNat = !Network.HavePublicAddress();
Network.InitializeServer(32, 26000, useNat);
}
void OnConnectedToServer () {
ip = "connected!";
SpawnMyself ();
}
void OnServerInitialized () {
ip = "created server!";
SpawnMyself ();
}
}