How to list the rooms that are there [PUN]

I am a extreme noob at this networking and GUI stuff and need some help.

I am trying to hook up my script to a button, that when you press it, it lists the all the available rooms that are there. I don’t know where to go from what I have. I also have another button that joins the game that has a room selected, but I don’t know how to do that either…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LobbyManger : MonoBehaviour
{
private CreateMatchManger Match;

private void Start()
{
    Match = GetComponent<CreateMatchManger>();
}

public void ToMatchCreate()
{
    SceneManager.LoadScene("Lobby Create Match");
}

public void ToMainMenu()
{
    SceneManager.LoadScene("Main Menu");
}

public void OnGUI()
{
    GetRoomsListed();
}

public void GetRoomsListed()
{
foreach(RoomInfo game in PhotonNetwork.GetRoomList())
{
if (GUI.Button(new Rect(10, 70, 100, 50), Match.RoomName.ToString())){
JoinRoom();
}
}
}

private void JoinRoom()
{
    PhotonNetwork.JoinRoom(Match.RoomName.ToString());
}

}

you can create delegate to join room button to selected room to join…

private void CreateRoomList()
{

    for (var i = RoomParent.transform.childCount - 1; i >= 0; i--)
    {
        // objectA is not the attached GameObject, so you can do all your checks with it.
        var obj = RoomParent.transform.GetChild(i);
        Destroy(obj.gameObject);
        // Optionally destroy the objectA if not longer needed
    }
 
       
    foreach (var item in PhotonNetwork.GetRoomList())
    {
        GameObject go = Instantiate(RoomPrefabUI) as GameObject;
        go.transform.SetParent(RoomParent.transform);
        go.transform.localScale = new Vector3(1, 1, 1);
        go.transform.GetChild(0).GetComponent<Text>().text = item.name;
        go.transform.GetChild(1).GetComponent<Text>().text = item.playerCount + "/" +item.maxPlayers;
        AttachOnClickEvent(go.transform.GetChild(2).gameObject.GetComponent<Button>(), item.name);
        Debug.Log("Create Room prefab");
    }
}

private void AttachOnClickEvent(Button button, string roomName)
{
    button.onClick.AddListener(() => OnJoinRoomClicked(roomName));
}

public void OnJoinRoomClicked(string roomName)
{
PhotonNetwork.JoinRoom(roomName);
}

Hi,

when using OnGUI() function there is an easy way to achieve what you want. This also doesn’t require any additional scripts e.g. the CreateMatchManager. You can try the following:

public void OnGUI()
{
    if (PhotonNetwork.insideLobby)
    {
        if (GUILayout.Button("Create Game"))
        {
            PhotonNetwork.CreateRoom(PhotonNetwork.player.NickName);
        }

        foreach (RoomInfo room in PhotonNetwork.GetRoomList())
        {
            if (GUILayout.Button(room.Name.ToString()))
            {
                PhotonNetwork.JoinRoom(room.Name);
            }
        }
    }
}

Please also make sure that you join the lobby after having successfully connected to Photon. You can do this by using OnConnectedToMaster() callback or - again the easy way - by navigating to the PhotonServerSettings file and check the Auto-Join Lobby option.