Connection already ready when adding players to Host

Working on a game which will have three different game modes, two which are single device and a third which allows access through the network. In order to simplify the game code its been structured to have a single scene with networking identities on all relevant objects. In doing so, I’m setting up a custom NetworkLobbyManager which can be used regardless of game mode.

The error I’m seeing is when attempting to add local players, specifically Lobby Players, to a started host. The process flow is below:

public class MyLobbyManager : NetworkLobbyManager {
	public void StartSingleHost() {
		maxConnections = 1;
		maxPlayers = 5;
		maxPlayersPerConnection = 5;
		autoCreatePlayer = false;
		StartHost();
	}
}

This configures and starts the host. On the subsequent frame, I’m attempting to add two local device players via:

MyLobbyManager lobbyMgr = FindObjectOfType<MyLobbyManager>();
lobbyMgr.TryToAddPlayer();

This performs as expected; however Unity throws the error: “A connection has already been set as ready. There can only be one.

I’ve tried with several different approaches:

  • ClientScene.AddPlayer ~ Same error
  • NetworkServer.AddPlayerForConnection ~Different error, I don’t recall the exact messaging

My original attempt is what is currently listed. After no results I used .NET Reflector to inspect how the NetworkLobbyManger had this setup. The key areas of interest in the NetworkLobbyManager are:

public class NetworkLobbyManager : NetworkManager {
	public override void OnStartServer() {
		if (this.lobbySlots.Length == 0)
			this.lobbySlots = new NetworkLobbyPlayer[this.maxPlayers];
		this.OnLobbyStartServer();
	}
	public virtual void OnLobbyStartServer() {}

	public override void OnStartHost(){
		this.OnLobbyStartHost();
	}
	public virtual void OnLobbyStartHost() { }

	private void OnGUI() {
		...
		this.TryToAddPlayer();
	}
}

The reference I found regarding this issue showed that a simple check is being made to see if the connection already exists and then exists, with an error logged, if that is the case. As queried in the reference and based upon my own inspection, it doesn’t look like this impacts anything; however, any errors in the log bother me. Inspecting further it does look as if StartHost() calls ConnectLocalClient() but tracing that is a veritable rats nest.

Is there a preferred method of starting a local host or connecting local players? Considering an implementation using a NetworkLobbyManager and the NetworkMangerHUD don’t yield the same results when adding players I can’t imagine this is a defect within Unity, unless there’s something within the NetworkLobbyManager which is not available in the subclass. Now that I think of it, I did notice a similar, unresolved, issue when attempting to extend the NetworkLobbyPlayer.

Any insight into the matter would be greatly appreciated.

It looks like this may be a result of a delay between when the Host is started and the Client becomes ready. I had tried individual and successive calls to:

if (ClientScene.ready) {
	ClientScene.AddPlayer(controllerId);
}
else {
	ClientScene.AddPlayer(NetworkClient.allClients[0].connection, controllerId);
}

Always with the same error message. After moving on and trying to resolve another issue with stopping and restarting the Host, I noticed that the error was no longer occurring. Moving the player creation logic to the Update method resolved the error. More specifically, I threw it behind:

public void Update() {
	if (!doneAddingPlayers && ClientScene.ready) {
		AddPlayer(controllerId);
		AddPlayer(controllerId);
		doneAddingPlayers = true;
	}
}

I’ll be doing some more inspection and if timing is the issue, creating a callback for player creation and adding that to MyLobbyManager once the client is ready.