UNET - Why couldn't I join a host as a client?

Please help me! I have already searched for answers in the internet but none of them were useful. Please help me solve it. Thanks in advance. This is the question:

  • When a host starts the game and the client tries to connect to the host, the JoinClient() method is called. This always worked. But for some strange reason, the client doesn’t connect to the host now, and I get some errors after pressing the “Join as client” button (I have built a custom UI) . For example, I got:

  • UNet Client Disconnect Error: NoResources

  • host id {0} has been already deleted

This is my script:

private GameObject PlayerPrefab; public GunController guncontroller; void OnEnable() {

  • if (PlayerPrefab = null)
  • return;
  • SceneManager.sceneLoaded += OnSceneLoaded;
  • }
    • public void SetUpMenu()
  • {
  • GameObject.Find(“ButtonStartHost”).GetComponent().onClick.RemoveAllListeners();
  • GameObject.Find(“ButtonStartHost”).GetComponent().onClick.AddListener(StartServer);
  • GameObject.Find(“ButtonJoinClient”).GetComponent().onClick.RemoveAllListeners();
  • GameObject.Find(“ButtonJoinClient”).GetComponent().onClick.AddListener(JoinClient);
  • }
  • public void StartServer()
  • {
  • NetworkServer.Reset();
  • singleton.StartHost();
  • Debug.Log("IP= " + singleton.networkAddress + " " + "Port = " + singleton.networkPort);
    • }
  • void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  • {
  • if(scene.name == “PlayWithFriend”)
  • {
  • SetUpMenu();
  • }
  • else
  • {
  • SetupDisconnectMenu();
  • }
  • }
  • public void JoinClient()
  • {
  • Debug.Log(“pressed”);
  • NetworkServer.Reset();
  • string Ip = GameObject.Find(“TextIP”).GetComponent().text;
  • string Port = GameObject.Find(“TextPort”).GetComponent().text;
  • if(Ip.Length > 0)
  • {
  • singleton.networkAddress = Ip;
  • }
  • if(Port.Length > 0)
  • {
  • int x;
  • int.TryParse(Port, out x);
  • singleton.networkPort = x;
  • }
  • singleton.StartClient();
    • }
  • public void SetupDisconnectMenu()
  • {
  • GameObject.Find(“ButtonDisconnect”).GetComponent().onClick.RemoveAllListeners();
  • GameObject.Find(“ButtonDisconnect”).GetComponent().onClick.AddListener(StopHostAndClient);
  • }
  • public void StopHostAndClient()
  • {
  • singleton.StopClient();
  • singleton.StopHost();
  • }

Extra Detail: I am building a simple 2d multiplayer shooting game. I am doing all this stuff on the same computer.

JoinClient - remove networkserver.reset() call this call will close all sockets, delete all connection and reinitialize library. Do them only on start and on finish…

Thanks, that did help a lot

1 Like