Photon - Cannot join same room
I have a created a simple multiplayer test code. This is the intended flow of the whole game:
Connect to Master
Connect to a random Room
If no room exists, create a new one.
Problem of this is that 2 clients cannot see each other's room even if it's already created. I purposely exported a build of the game so that it will function as another client. When I run the game on Unity first, it will create a new room since no room existed yet, however when I run my exported game moments after creating the room in Unity, I still get a "no room existed" and then the exported creates a new empty room - so there are actually 2 rooms now in total.
I tried creating a specific named room yet still, the same problem happens. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
namespace KingReigns
{
public class connectorHandler : MonoBehaviourPunCallbacks
{
public bool isConnectingToMaster;
public bool isConnectingToRoom;
public bool isConnectedToServer;
public TMP_Text txtStatus;
[SerializeField]
private GameObject btnConnectMaster, btnConnectRoom;
#region START
private void Start()
{
DontDestroyOnLoad(gameObject);
isConnectingToMaster = false;
isConnectingToRoom = false;
isConnectedToServer = false;
txtStatus.text = "Not Connected";
}
#endregion
#region UPDATE
private void Update()
{
if(btnConnectMaster !=null && btnConnectRoom != null)
{
if (!isConnectedToServer)
{
btnConnectMaster.SetActive(true);
btnConnectRoom.SetActive(false);
}
else
{
btnConnectMaster.SetActive(false);
btnConnectRoom.SetActive(true);
}
}
}
#endregion
#region ONCLICK_CONNECTMASTER
public void OnClick_ConnectToMaster()
{
PhotonNetwork.OfflineMode = false;
PhotonNetwork.NickName = "Proto1";
PhotonNetwork.GameVersion = "v1";
PhotonNetwork.ConnectUsingSettings();
isConnectingToMaster = true;
isConnectedToServer = false;
txtStatus.text = "Connecting to Server";
}
#endregion
#region CONNECTION OUTCOMES
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
isConnectingToMaster = false;
isConnectedToServer = true;
txtStatus.text = "Welcome to PlayRoom " + PhotonNetwork.NickName;
}
public override void OnDisconnected(DisconnectCause cause)
{
base.OnDisconnected(cause);
isConnectingToMaster = false;
isConnectedToServer = false;
txtStatus.text = "Connection to Server Failed!";
}
#endregion
#region ONCLICK_ROOM
public void onClick_ConnectToRoom()
{
//Checkpoint <- if not connected
if(!PhotonNetwork.IsConnected)
{
return;
}
//PhotonNetwork.JoinRoom("protoroom1");
PhotonNetwork.JoinRandomRoom();
isConnectingToRoom = true;
txtStatus.text = "Connecting to a Room";
}
#endregion
#region JOIN ROOM OUTCOMES
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
isConnectingToRoom = false;
txtStatus.text = "Connected! Players: " + PhotonNetwork.CurrentRoom.PlayerCount + " | Master: " + PhotonNetwork.IsMasterClient + " | Name: " + PhotonNetwork.CurrentRoom.Name;
//StartCoroutine(ForwardToRoom());
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
// !!!!!!! TEMPORARY !!!!!!! \\
// !!!! CREATE A ROOM LATER !!!! \\
base.OnJoinRandomFailed(returnCode, message);
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = 5, IsVisible = true });
}
/*
public override void OnJoinRoomFailed(short returnCode, string message)
{
base.OnJoinRoomFailed(returnCode, message);
PhotonNetwork.CreateRoom("protoroom1", new RoomOptions { MaxPlayers = 5 });
}
*/
public override void OnCreateRoomFailed(short returnCode, string message)
{
base.OnCreateRoomFailed(returnCode, message);
txtStatus.text = "Create Room Failed!";
Debug.Log(message);
}
IEnumerator ForwardToRoom()
{
yield return new WaitForSeconds(3);
SceneManager.LoadScene("Gameplay");
}
#endregion
}//End: Class
}//End: Namespace
EDIT: I'm using Photon v2.11
can you try adding this before you join a game:
PhotonNetwork.AuthValues = new Photon.Realtime.AuthenticationValues("Player" + Random.Range(0, 100));
PhotonNetwork.LocalPlayer.NickName = "Player" + Random.Range(0, 1000000);
If 2 players have the same values you cannot join the same room
Answer by rk_rathod · Jul 07, 2021 at 08:04 AM
it's simple follow this steps:
step 1 check your cloud region name (e.g. eu is your cloud region name)
if (_CloudRegiontext!=null) { _CloudRegiontext.text = $"Cloud Region : {PhotonNetwork.CloudRegion}"; }
step 2 check your other player cloud region name , if other player cloud region is not same so goto unity editor -> window -> photon server setting (highlight setting) then under dev region inputfield enter "eu".
step 3 start game print logs about current open names, and check room list is updating or not.
Now you connect in same room
Your answer
Follow this Question
Related Questions
Match making in Photon 0 Answers
Both players reach same time how to get result draw in photon unity 1 Answer
PhotonOnSerializeView() Only working sometimes 0 Answers
Racing game using PUN 2 0 Answers