Network.Connect() never returns InvalidPassword

Whenever Network.Connect is called with the wrong password provided, the returned NetworkConnectionError is NetworkConnectionError.NoError. And then in the Unity Console it displays these two errors:

  • “The remote system is using a password and has refused our connection because we did not set the correct password.”
  • “The connection request to 192.168.1.200:25001 failed. Are you sure the server can be connected to?”
  • Unity Console Window displaying errors

The returned value should be NetworkConnectionError.InvalidPassword so that I can deal with the mistake. The Unity errors shouldn’t even be happening.

I am using the Network.Connect(guid, pw) version to connect. If you provide the right password or set the server not to have a password, then everything works fine. I am testing on the same machine with a Build & Run instance and one in the Unity editor.

Update: I just tested with two builds just to rule it out and it has the the same result.

NetworkConnectionError connectError = Network.Connect(guid, pw);

if(connectError == NetworkConnectionError.NoError)
{
	Debug.Log("Connected to Server"); // Always called even if the wrong password is provided.
}
else if(connectError == NetworkConnectionError.InvalidPassword)
{
	Debug.Log("Invalid Password. Could not connect to server"); // Never gets called
}

What the heck is going on?

The problem is that Network.Connect() gives you the errors asynchronously. The only NetworkConnectionError’s returned are those that are immediate such as:

  • NetworkConnectionError.IncorrectParameters
  • NetworkConnectionError.EmptyConnectTarget

This means that you will not only NOT see any of the other errors including NetworkConnectionError.InvalidPassword when calling Network.Connect().

To actually see if you entered an invalid password or any of the other multitude of errors you need to wait for Network.OnFailedToConnect(). Or Network.OnConnectedToServer() meaning there were no errors and you were able to connect.

Here is a small script to get you started with error handling:

void OnConnectedToServer() {
	Debug.Log("Connected to server");
}

void OnFailedToConnect(NetworkConnectionError error) {
	Debug.Log("Could not connect to server: " + error);

	if(error = NetworkConnectionError.InvalidPassword)
		Debug.Log("Invalid Password Entered");
	else
		Debug.Log("Another error occured");
}