Unity & PhotonEngine. After scene reaload Joining to Lobby, CreateRoom and JoinRandom functions called repeatedly

I ran into a problem in the last step of a test project using Photon Network. When you first connect and join the room, everything goes without errors. However, after completing the match, exiting the room, and using LoadScene(), errors appear:

JoinLobby operation (229) not called because client is not connected or not yet ready, client state: JoiningLob <- in OnConnectedToMaster()

Through experience, I realized that the ConnectUsingSettings() methods and other Photon methods are called multiple times. But the connection to the lobby happens and I can create a room, but I immediately encounter MissingReferenceException errors.

I've seen a solution from guys who ran into this very same problem. The problems arose because of the events. Wherever this could happen, I unsubscribed from the events, but that doesn't help. What else can cause such problems, because I obviously missed something that prevents me from completely closing the scene during the transition?

Sorry for my language, used Google Translate

Code: LobbyManager.cs

private void StartConnect()
{
     PhotonNetwork.NickName = master.GameSettings.NickName;
     PhotonNetwork.GameVersion = master.GameSettings.NickName;
     PhotonNetwork.ConnectUsingSettings();
     PhotonNetwork.AutomaticallySyncScene = true;
}
public override void OnConnectedToMaster()
{
     Debug.Log("Connected to server");
     if(!PhotonNetwork.InLobby) PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
     onConnected.Invoke();//This use for show UIElements on Canvas
}

JoinRandomRoom class

public void OnClick_JoinRandomRoom()
{
     if (!PhotonNetwork.IsConnected) return;
              
     if (GameModeGlobalData.SelectedGameMode != null)
     {
           SetRoomOptions();
           PhotonNetwork.JoinRandomRoom(expectedRoomProperties, GameModeGlobalData.SelectedGameMode.MaxPlayers);
     }
}
    
public override void OnJoinRandomFailed(short returnCode, string message)
{
     Debug.Log("Join random failed: " + message + ". Room will be created...");
            _createRoomMenu.CreateAndJoinRoom();
}
    
public void SetRoomOptions()
{
     expectedRoomProperties[RoomData.GAME_MODE] = GameModeGlobalData.SelectedGameMode.GameModeName;
}

 
private void OnDisable()
{
      ShowPanels.RemoveAllListeners();
} 

And CreateRoom.cs

private ExitGames.Client.Photon.Hashtable _roomCustomProperties = new ExitGames.Client.Photon.Hashtable();
public void CreateAndJoinRoom()
    {
        if (!PhotonNetwork.IsConnected) return;
        if (GameModeGlobalData.SelectedGameMode != null)
        {
            RoomOptions roomOptions = GetCustomRoomOptions();
            roomOptions.CleanupCacheOnLeave = true;
            PhotonNetwork.CreateRoom(randomRoomName, roomOptions);
        }
    }
    public RoomOptions GetCustomRoomOptions()
    {
        RoomOptions options = new RoomOptions();
        options.MaxPlayers = _maxPlayer;
        options.IsOpen = true;
        options.IsVisible = true;

        string[] roomProperties = new string[]{ RoomData.GAME_MODE };
        _roomCustomProperties[RoomData.GAME_MODE] = GameModeGlobalData.SelectedGameMode.GameModeName;

        options.CustomRoomPropertiesForLobby = roomProperties;
        options.CustomRoomProperties = _roomCustomProperties;
        return options;
    }

The project has grown, and I blame myself for not testing it at the very beginning. Didn't think there would be problems at this stage

1 answer

  • answered 2022-05-05 09:19 NNNN

    Sorry for this post. Its resolved. For those who may encounter this in the future, in addition to unsubscribing from events, check all classes that inherit from MonoBehaviourPunCallbacks for overridden OnDisable() methods. Like this:

    public override void OnDisable()
    {
         base.OnDisable();
    }
    

    This in turn will call the

    PhotonNetwork.RemoveCallbackTarget(this);
    

    Also, from the documentation:

    Do not add new MonoBehaviour.OnEnable or MonoBehaviour.OnDisable. Instead, you should override those and call base.OnEnable and base.OnDisable.

    I forgot about it and used MonoBehaviour.OnDisable.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum