I am trying to allow my users to be able to host servers for my game. Hosting works great, until a user decides to stop their server, and then start a new one. The game does not allow this to happen, and calls a network error:

StartServer listen failed.

How do I get around this error?

Here is my current code for disabling the current server and starting a new one:

//disconnect client/server
NetworkManager.networkManager.Disconnect ();

//set the port
NetworkManager.networkManager.networkPort = port;
//start the server
NetworkManager.networkManager.StartServer ();
//set the map
ServerInfoManager.serverInfoManager.ChangeMap (map);

Here is NetworkManager.Disconnect():

public void Disconnect () {
    if (isServer == true) {
        StopServer ();

    if (isClient == true) {
        StopClient ();

The isServer, isClient, args[], and port variables have been set before these code sections.

  • \$\begingroup\$ This could be because port is set to a value that's in use or invalid. If it's set to the same value as before, then the previous instance is still using it. Try changing it to a different value (e.g. port+1) for debugging purposes. If the error goes away, you know that's the problem. \$\endgroup\$ – Peter Mar 23 '17 at 23:04
  • \$\begingroup\$ The port is the same as before but I thought that would be fine because I am stopping the server by calling Disconnect(); The port needs to be able to be the same because the user should be able to launch a server with the same port as before if they want to restart the server or something. \$\endgroup\$ – Brady W Mar 23 '17 at 23:53
  • \$\begingroup\$ I'm not saying changing ports is the solution. I'm saying changing ports will let you figure out the problem. If changing ports works, you know Disconnect doesn't close the port, at least not immediately.. \$\endgroup\$ – Peter Mar 24 '17 at 0:32
  • \$\begingroup\$ @Peter I can confirm that changing the port does work, so Disconnect () must not close the port \$\endgroup\$ – Brady W Mar 24 '17 at 4:40
  • \$\begingroup\$ Maybe you need shutdown instead docs.unity3d.com/ScriptReference/… \$\endgroup\$ – Peter Mar 24 '17 at 11:56

Figured it out myself. This was a Unity glitch and it has been patched in Unity version 5.6

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.