Question by
jawadzaib3 · Jun 24, 2019 at 04:22 PM ·
networkingpunroomjoin
Can't Join same Room in Photon Unity Network 2
I have created a simple Script to make connection between players using Photon Unity Network 2. When I connect using default settings and join a random room, PUN auto create a new room instead of calling an overriden method OnJoinRandomFailed
. 2nd Problem is if 2nd user try to connect using JoinRandomRoom
, it should connect with same room, which was created by first user. But PUN creates new room for every new user. Here is my Code for NetworkScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;
public class PhotonNetworkManager : MonoBehaviourPunCallbacks
{
[SerializeField] private Button masterConnectButton;
[SerializeField] private Button roomConnectButton;
private bool triesToConnectToMaster;
private bool triesToJoinRoom;
private void Start()
{
DontDestroyOnLoad(gameObject);
triesToConnectToMaster = false;
triesToJoinRoom = false;
}
private void Update()
{
if(masterConnectButton != null)
masterConnectButton.gameObject.SetActive(!PhotonNetwork.IsConnected && !triesToConnectToMaster);
if(roomConnectButton != null)
roomConnectButton.gameObject.SetActive(PhotonNetwork.IsConnected && !triesToConnectToMaster && !triesToJoinRoom);
}
public void OnButtonConnectToMaster()
{
PhotonNetwork.OfflineMode = false;
PhotonNetwork.NickName = "Player 1";
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.GameVersion = "v1";
triesToConnectToMaster = true;
PhotonNetwork.ConnectUsingSettings();
}
public void OnButtonJoinRoom()
{
if (!PhotonNetwork.IsConnected)
return;
triesToJoinRoom = true;
Debug.Log("Joining Room");
//PhotonNetwork.JoinOrCreateRoom("TestRoom", new RoomOptions { MaxPlayers = 20 }, TypedLobby.Default);
//PhotonNetwork.JoinRoom("TestingRoom2334234");
PhotonNetwork.JoinRandomRoom();
}
public override void OnDisconnected(DisconnectCause cause)
{
base.OnDisconnected(cause);
triesToConnectToMaster = false;
triesToJoinRoom = false;
Debug.Log(cause);
}
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
triesToConnectToMaster = false;
Debug.Log("Connected to Master");
}
public override void OnJoinedLobby()
{
base.OnJoinedLobby();
Debug.Log("Lobby Joined");
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
triesToJoinRoom = false;
Debug.Log("Master: " + PhotonNetwork.IsMasterClient + " | Players in Room: " + PhotonNetwork.CurrentRoom.PlayerCount);
//SceneManager.LoadScene("Game");
}
public override void OnCreatedRoom()
{
base.OnCreatedRoom();
Debug.Log("Room Created");
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
base.OnJoinRoomFailed(returnCode, message);
Debug.Log("Join room error: " +message);
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
base.OnJoinRandomFailed(returnCode, message);
Debug.Log("Random join room Error: " + message);
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = 20 });
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
base.OnCreateRoomFailed(returnCode, message);
Debug.Log("Create Room Error: "+ message);
triesToJoinRoom = false;
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
base.OnPlayerEnteredRoom(newPlayer);
Debug.Log("Player entered room");
}
}
Comment
Answer by flixtgi · Mar 18, 2020 at 03:05 AM
I am having this issue as well, did you get it resolved? @jawadzaib3
Your answer
![](https://koobas.hobune.stream/wayback/20220612203751im_/https://answers.unity.com/themes/thub/images/avi.jpg)