I’m trying to create a room using Photon PUN, but I’m getting an error:
CreateRoom failed. Client is on MasterServer (must be Master Server for matchmaking) but not ready for operations (State: PeerCreated). Wait for callback: OnJoinedLobby or OnConnectedToMaster.
Here’s the line where this error appears:
PhotonNetwork.CreateRoom(string.Format("[PUBLIC MATCH] {0}",
PhotonNetwork.LocalPlayer.ActorNumber + Random.Range(0, 999)),
new RoomOptions() {
MaxPlayers = (byte)maxPlayers,
IsVisible = false,
IsOpen = false,
CustomRoomProperties = roomOption,
CleanupCacheOnLeave = true,
CustomRoomPropertiesForLobby = properties
},
null);
What’s the cause of this error?
This error happens when you try to create a room before Unity fully connects to Photon, or while it’s doing something else (like already trying to join a room).
To fix this, make sure that you only ever call this function whenever the client is actually connected, either by checking PhotonNetwork.IsConnectedAndReady
, or, if you want the player to join a room automatically when the game starts, you can use the OnConnectedToMaster
callback:
Make sure your class inherits from MonoBehaviourPunCallbacks
, override the OnConnectedToMaster
method, and put your room creation code into the method:
public class YourClass : MonoBehaviourPunCallbacks {
public override void OnConnectedToMaster() {
PhotonNetwork.CreateRoom(string.Format("[PUBLIC MATCH] {0}",
PhotonNetwork.LocalPlayer.ActorNumber + Random.Range(0, 999)),
new RoomOptions() {
MaxPlayers = (byte)maxPlayers,
IsVisible = false,
IsOpen = false,
CustomRoomProperties = roomOption,
CleanupCacheOnLeave = true,
CustomRoomPropertiesForLobby = properties
},
null);
}
// ... rest of your class' code...
}