- Home /
How to Connect() in LAN, not working?
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 = "\n" + 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 ();
}
}
Answer by KdRWaylander · Jun 16, 2015 at 07:27 AM
Hi,
What ip adress have you tried ? Make sur to check that ip using "ipconfig -all"
Here is a script i use to connect locally on my network, it tries to connect first on the same computer "127.0.0.1" and then if it doesn't succeed, on the network
using UnityEngine;
using System.Collections;
public class ClientProps : MonoBehaviour {
private string serverIPLocal = "127.0.0.1";
private string serverIPRemote = "10.221.34.210";
private int serverPort = 25000;
// Use this for initialization
void Start () {
Connexion ();
}
private void Connexion () {
if (Network.peerType == NetworkPeerType.Disconnected) {
Network.Connect(serverIPLocal, serverPort);
}
if (Network.peerType == NetworkPeerType.Disconnected) {
Network.Connect(serverIPRemote, serverPort);
}
}
/* MESSAGES */
void OnConnectedToServer () {
// stuff
}
}
It may sound crazy but try passing only variables in your Connect and InitializeServer methods, not direct values. And check the ip ^^
I tried to pass only variables, but to no help. It's not the IP, as i have it show in the side panel which is active while you are prompted to either connect or host just after the game is launched. I tried every "192.168.0.x" from 0 to 9 and "127.0.0.1", even leaving the IP as "localhost", it keeps throwing the same error in the editor "The connection request to ip:port failed. Are you sure the server can be connected to?". I'm quite a begginer when it comes to networking (and mostly everything else lol) so i have no idea why it doesn't work now, since it did before Unity 5.0. Also, i've updated to 5.1 with the "new networking", but i'm quite afraid to use their scripts, both due of the lack of documentation and the fact that i like to know exactly what the hell is going on in the scripts i use (i also like to model my own stuff mostly, i don't trust other people enough for that hehe)
$$anonymous$$mmmm usually when i get this error is when i forgot to launch my server. You do have the debug log saying "created server!" ?
Yes, as you can see in my code, the player only spawns when the server initiates, so it's pretty obvious it started succesfully.
Try using two different strings: the 'ip' string you use to store "127.0.0.1" or else, and the one you use to display "connected" or "created server !" ?
Your answer
Follow this Question
Related Questions
Unet LAN Connection Function 0 Answers
UNet equivalent of OnDisconnectedFromServer 0 Answers
Commands and ClientRPC are not running when called 0 Answers
Multiple Cars not working 1 Answer