How to check and join a room with a specific name

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to check and join a room with a specific name

AlexLukanga
2020-01-30 21:57:56

I created a room with a name, and I would like to join this room

Comments

JohnTube
2020-01-31 13:01:04

Hi @AlexLukanga,

Thank you for choosing Photon!

Just call JoinRoom(roomName) and implement callbacks for success & failure (e.g. game is full, game is closed, game does not exist).
There is also JoinOrCreate method which attempts to join then, if the room is not found, creates it.

AlexLukanga
2020-02-01 18:09:23

I just creted a funtion that check room but i'm geting this error :smile:

Assets\Scripts\Connect.cs(66,45): error CS7036: There is no argument given that corresponds to the required formal parameter 'typedLobby' of 'PhotonNetwork.GetCustomRoomList(TypedLobby, string)'

using Photon.Pun;  
using Photon.Realtime;  
using System.Collections;  
using System.Collections.Generic;  
using TMPro;  
using UnityEngine;

public class Connect : MonoBehaviourPunCallbacks  
{  
    [SerializeField] private GameObject connectingPanel;  
    [SerializeField] private GameObject createRoomPanel;  
    [SerializeField] private GameObject startGamePanel;  
    [SerializeField] private GameObject adversaryPanel;

    [SerializeField] private TextMeshProUGUI mainServerText;  
    [SerializeField] private TextMeshProUGUI mainUserText;

    [SerializeField] private TextMeshProUGUI otherServerText;  
    [SerializeField] private TextMeshProUGUI otherUserText;

    [SerializeField] private TextMeshProUGUI infoServerText;

    private bool isConnected = false;

    private const string gameVersion = "0.1";  
    private const int maxPlayersPerRoom = 2;

    [SerializeField] private int correntNumber = 0;  
    [SerializeField] private int playerCount = 0;  
    [SerializeField] private string roomName;

    private void Awake() => PhotonNetwork.AutomaticallySyncScene = true;

    public void Update()  
    {  
        playerCount = PhotonNetwork.CurrentRoom.PlayerCount;  
        roomName = mainServerText.text;  
    }

    public void CreateServer()  
    {  
        createRoomPanel.SetActive(false);  
        connectingPanel.SetActive(true);  
        isConnected = true;

        PhotonNetwork.ConnectUsingSettings(gameVersion);  
    }  
    public override void OnConnectedToMaster()  
    {  
        if(playerCount == 0 && correntNumber == 0)  
        {  
            CreateARoom();  
        }  
    }  
    public void CreateARoom()   
    {  
        RoomOptions roomOptions = new RoomOptions();  
        roomOptions.IsVisible = false;  
        roomOptions.MaxPlayers = 2;  
        PhotonNetwork.CreateRoom(roomName, roomOptions, TypedLobby.Default);  
        Debug.Log("You create room: " + roomName);  
        correntNumber = 1;  
    }  
     public void JoinARoom()  
    {  
        RoomInfo[] roomInfo = PhotonNetwork.GetCustomRoomList(;  
        RoomInfo[] rooms = roomInfo;

        for (int i = 0; i < rooms.Length; i++)  
        {  
            if (rooms[i].Name == roomName)  
            {  
                Debug.Log("This room exists !");  
                PhotonNetwork.JoinRoom(roomName);  
                Debug.Log("You join in the room: " + roomName);  
                correntNumber = 2;  
            }  
        }

    }  
    public override void OnJoinedRoom()  
    {

        if (playerCount != maxPlayersPerRoom)  
        {  
            infoServerText.text = "Waiting for the opponent...";  
        }  
        else  
        {  
            infoServerText.text = "You are connected!";

            if (correntNumber == 1)  
            {  
                connectingPanel.SetActive(false);  
                 startGamePanel.SetActive(true);  
            }  
            else if (correntNumber == 2)  
            {  
                connectingPanel.SetActive(false);  
                adversaryPanel.SetActive(true);  
            }


        }  
        Debug.Log(playerCount.ToString());  
    }

}
Back to top