Home > Software design >  trying to limit the room max players but room options does not work in unity , photon
trying to limit the room max players but room options does not work in unity , photon

Time:08-05

I've created a game room that can be joined and i want it to have 2 players max . I used the code bellow but other players still can join even if the room has already 2 players . What can i do to make it work ?

public class CreateAndJoinRooms : MonoBehaviourPunCallbacks
{
    
    public TMP_InputField createInput;
    public TMP_InputField UserNameInput;
    public TMP_InputField joinInput;


    public void CreateRoom()
    {
        if (string.IsNullOrEmpty(UserNameInput.text) == false && string.IsNullOrEmpty(createInput.text) == false)
        { 
            PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;
            RoomOptions roomOptions = new RoomOptions();
            roomOptions.MaxPlayers = 2;

            PhotonNetwork.CreateRoom(createInput.text);
        }
        else Debug.LogError("Username or room name is empty");
            
             
      

    }
    
    public void JoinRoom()
    {



        if (string.IsNullOrEmpty(UserNameInput.text) == false)
        {
            PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;
            PhotonNetwork.JoinRoom(joinInput.text);
        }
        else Debug.LogError("Username or room name is empty");
        


    }

    public override void OnJoinedRoom()
    {

        
        PhotonNetwork.LoadLevel("Game");
        

    }

}
 

CodePudding user response:

You need to pass the roomOptions as a parameter to the CreateRoom function.

PhotonNetwork.LocalPlayer.NickName = UserNameInput.text;

RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 2;

PhotonNetwork.CreateRoom(createInput.text, roomOptions, null);
  • Related