JoinRandomRoom does nothing and gives sometimes an error message

Options
Good day everyone,

im trying to join a random room but nothing happens. Sometimes Il get the following error message: JoinRandomRoom failed. Client is on MasterServer (must be Master Server for matchmaking) but not ready for operations (State: PeerCreated). Wait for callback: OnJoinedLobby or OnConnectedToMaster.

My code:
public class Launcher : MonoBehaviourPunCallbacks {
    public static Launcher launcher;

    [Header("Room Settings")]
    public byte maxPlayersPerRoom;

    [Header("Dependencies")]
    public GameObject btnMultiplayer;
    public GameObject btnJoinRandomRoom;
    public GameObject btnLeaveRoom;
    public GameObject btnDisconnect;
    public GameObject lblConnecting;

    private string gameVersion;

    private void Awake() {
        Debug.Log("Network: Trying to initialize launcher");
        launcher = this;
        gameVersion = "1";
        PhotonNetwork.AutomaticallySyncScene = true;
        Debug.Log("Network: Launcher initialized");
    }

    public void Connect() {
        Debug.Log("Network: Trying to connect to server");
        SetLauncherElementsVisiblity(lblConnectingActive: true);
        PhotonNetwork.ConnectUsingSettings();
        PhotonNetwork.GameVersion = gameVersion;
    }

    public override void OnConnectedToMaster() {
        Debug.Log("Network: Successfully connected to master server");
        SetLauncherElementsVisiblity(btnJoinRandomRoomActive: true, btnDisconnectActive: true);
    }

    public void JoinRandomRoom() {
        Debug.Log("Network: Trying to join a random room");
        SetLauncherElementsVisiblity();
        PhotonNetwork.JoinRandomRoom();
    }

    public override void OnJoinedRoom() {
        Debug.Log("Network: Successfully joined a room");
        SetLauncherElementsVisiblity(btnLeaveRoomActive: true);
    }

    public override void OnJoinRoomFailed(short returnCode, string message) {
        Debug.LogFormat("Network: Joining a random room failed\nError Code: {0}\nError Message: {1}", returnCode, message);
        CreateRoom();
    }

    public void CreateRoom() {
        Debug.Log("Network: Trying to create a new room");
        string roomName = "Room " + Random.Range(0, 1000);
        RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = maxPlayersPerRoom };
        PhotonNetwork.CreateRoom(roomName, roomOps);
    }

    public override void OnCreatedRoom() {
        Debug.Log("Network: Successfully created a new room");
        SetLauncherElementsVisiblity(btnLeaveRoomActive: true);
    }

    public override void OnCreateRoomFailed(short returnCode, string message) {
        Debug.LogFormat("Network: Creating a new room failed\nError Code: {0}\nError Message: {1}", returnCode, message);
        CreateRoom();
    }

    public void LeaveRoom() {
        Debug.Log("Network: Trying to leave the room");
        SetLauncherElementsVisiblity();
        PhotonNetwork.LeaveRoom();
    }

    public override void OnLeftRoom() {
        Debug.Log("Network: Successfully left the room");
        SetLauncherElementsVisiblity(btnJoinRandomRoomActive: true, btnDisconnectActive: true);
    }

    public void Disconnect() {
        Debug.Log("Network: Trying to disconnect from server");
        SetLauncherElementsVisiblity();
        PhotonNetwork.Disconnect();
    }

    public override void OnDisconnected(DisconnectCause cause) {
        Debug.Log("Network: Successfully disconnected from server");
        SetLauncherElementsVisiblity(btnMultiplayerActive: true);
    }

    private void SetLauncherElementsVisiblity(bool btnMultiplayerActive = false, bool btnJoinRandomRoomActive = false, bool btnLeaveRoomActive = false, bool btnDisconnectActive = false, bool lblConnectingActive = false) {
        btnMultiplayer.SetActive(btnMultiplayerActive);
        btnJoinRandomRoom.SetActive(btnJoinRandomRoomActive);
        btnLeaveRoom.SetActive(btnLeaveRoomActive);
        btnDisconnect.SetActive(btnDisconnectActive);
        lblConnecting.SetActive(lblConnectingActive);
    }
}

Most of it are just callbacks to catch. I call the method JoinRandomRoom with a button.

I will appreciate every help.

Have a good day!

Comments

  • Conspyra
    Options
    Solution: name mismatch, it should be called OnJoinRandomFailed and not OnJoinRoomFailed.

    Thanks anyways