Question by
ErayT · Jun 17, 2016 at 11:34 PM ·
networkingserverclientlocalhost
Fail to Connect to NetworkServer
It looks easy but I cannot figure out why I cannot connect to my server in local.
Simple Server Code:
void Start() {
if (NetworkServer.Listen(15937)) {
Debug.Log("Server Created!");
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
} else {
Debug.Log("Failed To Create Server!");
}
}
void OnConnected(NetworkMessage netMsg) {
Debug.Log("Client connected");
}
Simple Client Code:
void Start () {
client = new NetworkClient();
client.Connect("127.0.0.1", 15937);
}
void Update () {
if (client.isConnected) {
Debug.Log("Connected to server!");
} else {
Debug.Log("Failed to Connected to server!");
}
}
PS: I get a message saying :
NetworkManager detected a script reload in the editor. This has caused the network to be shut down. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
I am not sure if it causes the problem or not. Can someone lead me to the solution?
Comment
Answer by hasanbayat · Feb 28, 2017 at 12:37 PM
If you have any scripts that's derived (extends) from NetworkManager, you should remove Awake method from that and put your initialization in Start or OnStartClient.
See this thread for more information.