- Home /
Not Connecting to Server
So, i'm making a game in Unity. It's multiplayer. For my specific 'needs' I had to create my own Network Manager. That's what I did. Everything appears to work fine, and it does, except for one thing: It's not connected to the server. Even though i've set up hosting, server running, and connecting, it supposedly is never actually connected. Please help. Here's the code for my Network Manager class: using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.SceneManagement;
enum PlayMenuMode { Home, Connect, Server, Connected, None }
public class PlayMenu : NetworkManager {
PlayMenuMode menu = PlayMenuMode.Home;
NetworkClient client;
int port = 4855;
string ip = "127.0.0.1";
short playerConnection;
void OnGUI()
{
if(menu == PlayMenuMode.Home)
{
if(GUILayout.Button("Singleplayer/LAN"))
{
menu = PlayMenuMode.None;
Host();
}
if(GUILayout.Button("Connect"))
{
menu = PlayMenuMode.Connect;
}
if(GUILayout.Button("Start Server"))
{
menu = PlayMenuMode.Server;
}
}
if(menu == PlayMenuMode.Connect)
{
GUILayout.Label("IP To Connect");
ip = GUILayout.TextArea(ip);
GUILayout.Label("Port To Use");
port = int.Parse(GUILayout.TextArea(port.ToString()));
if(GUILayout.Button("Connect")){
Connect();
menu = PlayMenuMode.Connected;
}
if(GUILayout.Button("Back"))
menu = PlayMenuMode.Home;
}
if(menu == PlayMenuMode.Server)
{
GUILayout.Label("Port:");
port = int.Parse(GUILayout.TextArea(port.ToString()));
if(GUILayout.Button("Start Server")){
NetworkServer.Listen(port);
menu = PlayMenuMode.None;
}
if(GUILayout.Button("Back"))
menu = PlayMenuMode.Home;
}
if(menu == PlayMenuMode.Connected)
{
Transform regionManager = GameObject.Find("_REGIONS").transform;
for(int i = 0; i < regionManager.childCount; i++)
{
if(GUILayout.Button(regionManager.GetChild(i).name))
{
menu = PlayMenuMode.None;
SceneManager.LoadSceneAsync(regionManager.GetChild(i).GetComponent<RegionController>().objectSourceScene, LoadSceneMode.Additive);
GameObject playerGO = (GameObject)Instantiate(Resources.Load("Player"), Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(client.connection, playerGO, playerConnection);
playerGO.transform.tag = "Local Player";
Camera.main.gameObject.SetActive(false);
playerGO.transform.FindChild("Camera").gameObject.SetActive(true);
}
}
}
}
void Host()
{
NetworkServer.Listen(4855);
client = ClientScene.ConnectLocalServer();
client.RegisterHandler(MsgType.Connect, OnConnected);
}
void Connect()
{
client = new NetworkClient();
client.RegisterHandler(MsgType.Connect, OnConnected);
client.Connect(ip, port);
}
public void OnConnected(NetworkMessage msg)
{
Debug.Log("Connected To Server");
menu = PlayMenuMode.Connected;
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerID)
{
playerConnection = playerControllerID;
}
}
I also get these errors: Local invoke: Failed to find local connection to invoke handler on [connectionId=0] for MsgId:12 Local invoke: Failed to find local connection to invoke handler on [connectionId=0] for MsgId:4
And I get these, but i'm pretty sure it's not from the net code: GetLocalizedString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
ArgumentException: GetLocalizedString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. TreeEditor.TreeGroupRoot..cctor ()
I also have this problem and hope someone can answer it. Also, the first run is fine (OnStartHost and OnStopHost), and the second time something goes wrong
Your answer
Follow this Question
Related Questions
uNet Connection to IPv6 [Networking] 0 Answers
Unity 5 Multiplayer - Changing When Players Can Connect? 0 Answers
UNET - Keep connection to client when changing scenes 1 Answer
Load scene in unet behind a loading screen 1 Answer
what is wrong here ? 0 Answers