Home > Net >  does'nt join the room || Photon Pun
does'nt join the room || Photon Pun

Time:03-25

I am recently working on my multiplayer game Using Unity3d and Photon PUN2;

I want to make a system where player clicks the button to join the room if it is not avilabe then create;

When staringt the game from two players (parrelSync) from p1 it just make a room normal (because in starting no rooms are available)

form p2 it does not join the room

here is the code

        string roomID = "002";
        string maxPlayers = "10";
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers = (byte)int.Parse(maxPlayers);
        if(PhotonNetwork.CurrentRoom != null){
            PhotonNetwork.JoinRoom(roomID);
        }else{
            PhotonNetwork.CreateRoom(roomID, roomOptions);
        }
    }

here is what i logged form p1

here is what i logged from p2

CodePudding user response:

PhotonNetwork.CurrentRoom returns the room which you are currently in, which will always be null in your case.

Instead of checking for PhotonNetwork.CurrentRoom just use PhotonNetwork.JoinRoom and also override the OnJoinRoomFailed() which will be called when we attempt to join the room with roomID which does not exist yet, and in it just create new room.

Something like following would work

private string roomID = "002";

private void YourMethod() {
    string maxPlayers = "10";
    RoomOptions roomOptions = new RoomOptions();
    roomOptions.MaxPlayers = (byte) int.Parse(maxPlayers);

    PhotonNetwork.JoinRoom(roomID);
}

public override void OnJoinRoomFailed(short returnCode, string message) {
    PhotonNetwork.CreateRoom(roomID);
}

And to override OnJoinRoomFailed() make sure your script is derived from MonoBehaviourPunCallbacks class

  • Related