I use Distributed Authority. Below is the function that I use to create a session.
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
networkManager.OnSessionOwnerPromoted += OnSessionOwnerPromoted;
networkManager.OnClientDisconnectCallback += OnClientDisconnect;
networkManager.OnClientConnectedCallback += OnClientConnected;
networkManager.OnTransportFailure += OnTransportFailure;
networkManager.OnClientStopped += OnClientStopped;
}
public async Task CreateLobbyAsync(bool isPrivate)
{
OnCreateLobbyStarted?.Invoke(this, EventArgs.Empty);
SessionOptions options = new SessionOptions
{
MaxPlayers = GameMechanics.MaxPlayerCount,
IsLocked = false,
IsPrivate = isPrivate,
PlayerProperties = GetPlayerProperties()
}.WithDistributedAuthorityNetwork();
try
{
activeSession = await MultiplayerService.Instance.CreateSessionAsync(options);
if (activeSession == null)
{
Debug.LogError("Failed to create session: returned null.");
OnCreateLobbyFailed?.Invoke(this, EventArgs.Empty);
return;
}
Debug.Log($"Session {activeSession.Id} created! Join code: {activeSession.Code} {activeSession.Host} {activeSession.PlayerCount}");
}
catch (Exception e)
{
Debug.LogError($"Session creation failed: {e}");
OnCreateLobbyFailed?.Invoke(this, EventArgs.Empty);
}
}
As you can see, it’s pretty generic. But sometimes, seemingly at random, the player client gets stuck at trying to connect after creating the session. Normally, when everything works fine, I receive events from OnClientConnected and OnSessionOwnerPromoted after creating the session. But sometimes, that never happens. I wait for couple of minutes and client disconnects after reaching maximum connection attemps. I’ve also tracked Transport events. NetworkEvent.Connect is also never fired from the Transport on NetworkManager when this bug happens. I’ve spent days but I couldn’t even consistently reproduce the issue. I tried different pc, wi-fi etc. but I’m pretty sure that the issue is not about my connection or firewall. This is also what my connection manager looks like. I’ve tried everything I can think of and still couldn’t the problem.
