Unet error code : unity host id out of bound id -1 max id should be greater 0 and less than 2

Tried to run on 2018.2.0f2 and 2017.2.0f3
That’s now 3 days I’m trying to get rid of this error. I’ve tried to allow unity in my firewalls, uninstall and install back again unity, to see if i’ve modified something wrong. But I can’t even see what the issue is.
Here is the complete error :


unity host id out of bound id {-1} max id should be greater {0} and less than {2}
UnityEngine.Networking.NetworkDiscovery:Update()


and here is the code I us for the NetworkManager and the NetworkDiscovery :

NetworkManager :

	public bool runAsServer = true;

    private CustomNetworkDiscovery netDiscovery;

	void Start () {

        netDiscovery = GetComponent<CustomNetworkDiscovery>();
        netDiscovery.Initialize();

		if (runAsServer) {
			SetupServer();
		} else {
			SetupClient();
		}
	}

    void netDiscovery_OnReceivedNetworkBroadcast(string fromAddress, string data)
    {
        Debug.Log("Received fromAddress:" + fromAddress);
        Debug.Log("Received data:" + data);

        networkAddress = fromAddress;
        StartClient();
        netDiscovery.StopBroadcast();
    }

	void SetupServer(){
        
        networkAddress = "localhost";
		StartHost();

        netDiscovery.StartAsServer();

	}

	void SetupClient(){

        netDiscovery.OnReceivedNetworkBroadcast += netDiscovery_OnReceivedNetworkBroadcast;
        netDiscovery.StartAsClient();
	}

    override public void OnClientDisconnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);

        StopClient();
        enabled = false;
        netDiscovery.enabled = false;
        Invoke("ProcessDisconnect", 0.5f);
    }
    
    void ProcessDisconnect()
    {
		enabled = true;

		netDiscovery.enabled = true;
        netDiscovery.Initialize();
        netDiscovery.StartAsClient();
    }

NetworkDiscovery :

    public delegate void BroadcastEvent(string fromAddress, string data);

    public event BroadcastEvent OnReceivedNetworkBroadcast = delegate { };

    override public void OnReceivedBroadcast(string fromAddress, string data)
    {
        OnReceivedNetworkBroadcast(fromAddress, data);
    }

Regarding this infuriating problem caused by Unity’s shoddy network code.

  • Just on your PCs/Macs, check you are getting good internet connection (ie, can you get to ordinary web sites fast)

  • Ensure that in the Unity project, you have “run in background” on

  • Often it will work “one way and not the other way”. Swap around your two PCs/Macs as client/server … and it will work the other way. Really stupid and infuriating.

And for late 2018 Unity introduced a new problem:

https://forum.unity.com/threads/networkmanager-error-server-client-disconnect-error-1.439245/#post-3754939

on that essay carefully note the “aside” added in RED - that will fix the problem in many cases. Infuriating!