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