Every player create separate room not join the available room

Hi Guys! I am a newbie on photon and using it for the first time in the unity multiplayer game. Every player creates a separate room and does not join the available room.

Here is script:


using DG.Tweening;

using ExitGames.Client.Photon;

using Photon.Pun;

using Photon.Realtime;

using UnityEngine;

using UnityEngine.UI;


public class Photon_Handler : MonoBehaviourPunCallbacks

{

  public static Photon_Handler instance;


  public Transform Connecting_image;

  public Text Connection_Status;

  public Text Room_Name;

  string RoomName;

  public int No_Of_Players;


  Hashtable room_Properties = new Hashtable();


  private void Awake()

  {

    instance = this;

  }

  // Start is called before the first frame update

  void Start()

  {

    if(!PhotonNetwork.IsConnectedAndReady)

    {

      start_Connection();

    }

    else

    {

      Connecting_image.gameObject.SetActive(false);

      Connection_Status.gameObject.SetActive(true);

      Gameplay_Handler.instance.Play_btn.interactable = true;

    }

     

  }


  // Update is called once per frame

  void Update()

  {

    if(Connecting_image.gameObject.activeInHierarchy)

    {

      Connecting_image.localEulerAngles = new Vector3(0, 0, Connecting_image.localEulerAngles.z - 3);

    }

    if(Gameplay_Handler.instance.Lobby_Panel)

    {

      Gameplay_Handler.instance.Lobby_Spinner_Image.transform.localEulerAngles = new Vector3(0, 0, Gameplay_Handler.instance.Lobby_Spinner_Image.transform.localEulerAngles.z - 3);

    }


  }

  void start_Connection()

  {

    Connection_Status.gameObject.SetActive(false);

    Connecting_image.gameObject.SetActive(true);

    Gameplay_Handler.instance.Play_btn.interactable = false;

    PhotonNetwork.ConnectUsingSettings();

  }

  public override void OnConnectedToMaster()

  {

    Connecting_image.gameObject.SetActive(false);

    Connection_Status.gameObject.SetActive(true);

    Gameplay_Handler.instance.Play_btn.interactable = true;

    Debug.Log(PhotonNetwork.LocalPlayer.NickName + " is connected to photon");


  }

  public override void OnDisconnected(DisconnectCause cause)

  {

    start_Connection();

  }

  public void Leave_Room()

  {

    if(PhotonNetwork.InRoom)

    {

      PhotonNetwork.LeaveRoom();

    }

  }


  public void Join_Room(Hashtable Mode)

  {

    room_Properties = Mode;

    int Players_for_Room = 0;

    foreach (string s in Mode.Keys)

    {

      Players_for_Room = (int)Mode[s];

      break;

    }

    PhotonNetwork.JoinRandomRoom(Mode, (byte)No_Of_Players);

    Debug.Log("Player join room");

  }

  public override void OnJoinRandomFailed(short returnCode, string message)

  {

    Debug.Log("Room creation failed with the code " + returnCode + " and with message " + message);

    RoomName = "TC_" + Random.Range(19999, 99999);

    RoomOptions roomOptions = new RoomOptions();

    roomOptions.IsVisible = true;

    roomOptions.IsOpen = true;

    int Players_for_Room = 0;

    string Name_for_Lobby = "";

    foreach (string s in room_Properties.Keys)

    {

      Name_for_Lobby = s;

      Players_for_Room = (int)room_Properties[s];

      break;

    }

    roomOptions.MaxPlayers = (byte)No_Of_Players;

    string[] Lobby_identifier = new string[1];

    Lobby_identifier[0] = Name_for_Lobby;

    roomOptions.CustomRoomPropertiesForLobby = Lobby_identifier;

    roomOptions.CustomRoomProperties = room_Properties;

  

    PhotonNetwork.JoinOrCreateRoom(RoomName, roomOptions, TypedLobby.Default);

    Room_Name.text = RoomName;

     


  }

  public override void OnJoinedRoom()

  {

    Gameplay_Handler.instance.Lobby_Text.text = "WAITING FOR PLAYERS: " + PhotonNetwork.CurrentRoom.PlayerCount + "/" + PhotonNetwork.CurrentRoom.MaxPlayers;

    Debug.Log("Room Max player1:" + PhotonNetwork.CurrentRoom.MaxPlayers);

    Debug.Log(PhotonNetwork.LocalPlayer.NickName + "joined to " + PhotonNetwork.CurrentRoom.Name);

    if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)

    {

      Invoke(nameof(_onEnteringRoom), 3);

    }



  }

  public override void OnPlayerEnteredRoom(Photon.Realtime.Player newPlayer)

  {

    Gameplay_Handler.instance.Lobby_Text.text = "WAITING FOR PLAYERS: " + PhotonNetwork.CurrentRoom.PlayerCount + "/" + PhotonNetwork.CurrentRoom.MaxPlayers;

    Debug.Log("Player name:" + PhotonNetwork.NickName);

    Debug.Log("Room Max player2:" + PhotonNetwork.CurrentRoom.MaxPlayers);

    if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)

    {

      Invoke(nameof(_onEnteringRoom), 3);

    }

  }


  void _onEnteringRoom()

  {

    PhotonNetwork.CurrentRoom.IsVisible = false;

    PhotonNetwork.CurrentRoom.IsOpen = false;

    Gameplay_Handler.instance.Lobby_Panel.SetActive(false);

    Gameplay_Handler.instance.Menu_Panel.SetActive(false);

    Gameplay_Handler.instance.Start_Match();

  }


  public override void OnPlayerLeftRoom(Photon.Realtime.Player otherPlayer)

  {

    if (PhotonNetwork.CurrentRoom.PlayerCount == 1)

    {

      if (Gameplay_Handler.instance.isMatchStarted)

      {

        Gameplay_Handler.instance.Menu_Panel.SetActive(true);

        //Gameplay_Handler.instance.Pause_Panel.SetActive(true);

        Gameplay_Handler.instance.Results_Panel.SetActive(true);

      }

    }

  }

}

Comments

  • I don't know the answer but let me suggest that you edit that message and insert all the code as "code" so it formats in a more readable form.

  • Thanks for the comment. Now the problem is fixed.

This discussion has been closed.