How would I create a waiting room for my game?

Options

Hi, I am attempting to learn multiplayer for a game I want to make, I am currently trying to make sure that when you join a room, it waits for one more person to join (The game starts when two people join the room) however, the following code does not do what I thought, for a start, when one person joins its fine, but when the other person joins, they get sent to the "GameScene" but the master client doesnt, instead unity continuosly opens the "GameScene" in the heirachy repeatedly, as seen in the following screenshot where the client who created the room is on the right (the master client):


using UnityEngine;
using Photon.Pun;
public class RoomWaiting : MonoBehaviourPunCallbacks
{
    void Update()
    {
        if(PhotonNetwork.IsMasterClient && PhotonNetwork.PlayerList.Length == 2)
        {
            PhotonNetwork.LoadLevel("GameScene");
        }
    }
}

I also put this in my launcher script:

        void Awake()
        {
            // this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
            PhotonNetwork.AutomaticallySyncScene = true;
        }

any help would be very great, thankyou!

Answers

  • Bassem
    Options

    Hello I know this is late but in case someone stumbles on this, Don't load level on the update function, instead use photon callback OnPlayerEnteredRoom Like this

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
            if(PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == 2)
            {
                PhotonNetwork.LoadLevel("GameScene");
            }
    }