0

I'm making a multiplayer game, when I create a room, there is an error, but I can still enter the room. Does the error affect the room? or do I just let it go? or is there something wrong with my coding? please help me fix it.

This is the error :

CreateRoom failed. Client is on MasterServer (must be Master Server for matchmaking)but not ready for operations (State: Joining). Wait for callback: OnJoinedLobby or OnConnectedToMaster. UnityEngine.Debug:LogError (object) Photon.Pun.PhotonNetwork:CreateRoom (string,Photon.Realtime.RoomOptions,Photon.Realtime.TypedLobby,string[]) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetwork.cs:1782) Launcher:CreateRoom (int) (at Assets/Script/Launcher.cs:63) Launcher:b__13_1 () (at Assets/Script/Launcher.cs:35) UnityEngine.EventSystems.EventSystem:Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

This is the code :

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

public class Launcher : MonoBehaviourPunCallbacks
{    
    public static Launcher Instance;
    [SerializeField] TMP_InputField roomNameInputField;
    [SerializeField] TMP_Text errorText;
    [SerializeField] TMP_Text roomNameText;
    [SerializeField] Transform roomListContent;
    [SerializeField] GameObject roomListItemPrefab;
    [SerializeField] GameObject PlayerListItemPrefab;
    [SerializeField] Transform playerListContent;
    [SerializeField] GameObject startGameButton;
    public GameObject twoPlayerButton;
    public GameObject threePlayerButton;
    public GameObject fourPlayerButton;
    

    void Awake(){
        Instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Connecting to Master");
        PhotonNetwork.ConnectUsingSettings();
        twoPlayerButton.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => CreateRoom(2));
        threePlayerButton.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => CreateRoom(3));
        fourPlayerButton.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => CreateRoom(4));

    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("Connected to Master");
        PhotonNetwork.JoinLobby();
        PhotonNetwork.AutomaticallySyncScene = true;
    }
     public override void OnJoinedLobby()
    {   
        MenuSetting.Instance.OpenMenu("firstmenu");
        Debug.Log("Joined Lobby");
    }
      public void CreateRoom(int numPlayers){

        if(string.IsNullOrEmpty(roomNameInputField.text)){
            return;
        }
        
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers = (byte)numPlayers;
        ExitGames.Client.Photon.Hashtable customProperties = new ExitGames.Client.Photon.Hashtable();
        customProperties.Add("MinPlayers", 2);
        roomOptions.CustomRoomProperties = customProperties;

        PhotonNetwork.CreateRoom(roomNameInputField.text + numPlayers, roomOptions);
        
        MenuSetting.Instance.OpenMenu("Loading");
    }

    
    public override void OnJoinedRoom()
    {
        MenuSetting.Instance.OpenMenu("room");
        roomNameText.text = PhotonNetwork.CurrentRoom.Name;

        Player[] players = PhotonNetwork.PlayerList;

        foreach(Transform child in playerListContent)
        {
            Destroy(child.gameObject);
        }

        for(int i = 0; i < players.Count(); i++){
            Instantiate(PlayerListItemPrefab, playerListContent).GetComponent<PlayerListItem>().SetUp(players[i]);
        }

        
        if (players.Count() >= 2)
        {
            startGameButton.SetActive(PhotonNetwork.IsMasterClient); //only show the button if there are more than 2 players
        }
        else
        {
            startGameButton.SetActive(false); // hide the button if there are less than 2 players
        }

    }

    public override void OnMasterClientSwitched(Player newMasterClient)
    {
        startGameButton.SetActive(PhotonNetwork.IsMasterClient);
    }

     public override void OnCreatedRoom()
    {
        Debug.Log("Room created successfully!");
    }

    public override void OnCreateRoomFailed(short returnCode, string message)
    {
        errorText.text = "Room Creation Failed: " + message;
        MenuSetting.Instance.OpenMenu("error");
    }

    public void StartGame()
    {
        PhotonNetwork.LoadLevel(1);
    }
    public void LeaveRoom()
    {
        PhotonNetwork.LeaveRoom();
        MenuSetting.Instance.OpenMenu("main_menu");
    }

    public void JoinRoom(RoomInfo info ){
        PhotonNetwork.JoinRoom(info.Name);
        MenuSetting.Instance.OpenMenu("loading");
    }

    public override void OnLeftRoom()
    {
        MenuSetting.Instance.OpenMenu("loading");

    }

    public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        foreach(Transform trans in roomListContent){
            Destroy(trans.gameObject);
        }
        for(int i = 0; i < roomList.Count; i++){
            if(roomList[i].RemovedFromList)
                continue;
            Instantiate(roomListItemPrefab, roomListContent).GetComponent<RoomListItem>().SetUp(roomList[i]);
        }
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        Instantiate(PlayerListItemPrefab, playerListContent).GetComponent<PlayerListItem>().SetUp(newPlayer);
    }
}

Takarii
  • 1,612
  • 2
  • 18
  • 29

0 Answers0