Hey Guys! I’m using Photon PUN 2 to make a multiplayer FPS game.
In the lobby, where all the players are waiting for the host to start the game, I want it that so when the host clicks on a button (which has the map name on it) and start the game in that scene, bringing all the players to it.
How do I do this?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
public class Launcher : MonoBehaviourPunCallbacks
{
public static Launcher Instance;
[SerializeField] TMP_InputField roomNameInputField;
[SerializeField] TMP_Text errorText;
[SerializeField] TMP_Text roomNameText;
[SerializeField] Transform roomListContent;
[SerializeField] GameObject roomListItemPrefab;
[SerializeField] Transform playerListContent;
[SerializeField] GameObject PlayerListItemPrefab;
[SerializeField] GameObject startGameButton;
void Awake()
{
Instance = this;
}
void Start()
{
Debug.Log("Connecting to Master");
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
Debug.Log("Connected to Master");
PhotonNetwork.JoinLobby();
PhotonNetwork.AutomaticallySyncScene = true;
}
public override void OnJoinedLobby()
{
MenuManager.Instance.OpenMenu("title");
Debug.Log("Joined Lobby");
PhotonNetwork.NickName = "Player " + Random.Range(0, 1000).ToString("0000");
}
public void CreateRoom()
{
if(string.IsNullOrEmpty(roomNameInputField.text))
{
return;
}
PhotonNetwork.CreateRoom(roomNameInputField.text);
MenuManager.Instance.OpenMenu("loading");
}
public override void OnJoinedRoom()
{
MenuManager.Instance.OpenMenu("room");
roomNameText.text = PhotonNetwork.CurrentRoom.Name;
Player[] players = PhotonNetwork.PlayerList;
foreach(Transform child in playerListContent)
{
Destroy(child.gameObject);
}
for(int i = 0; i < players.Length; i++)
{
Instantiate(PlayerListItemPrefab, playerListContent).GetComponent<PlayerListItem>().SetUp(players[i]);
}
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
errorText.text = "Room Creation Failed: " + message;
MenuManager.Instance.OpenMenu("error");
}
public void StartGame()
{
PhotonNetwork.LoadLevel(1);
}
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
MenuManager.Instance.OpenMenu("loading");
}
public void JoinRoom(RoomInfo info)
{
PhotonNetwork.JoinRoom(info.Name);
MenuManager.Instance.OpenMenu("loading");
}
public override void OnLeftRoom()
{
MenuManager.Instance.OpenMenu("title");
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
foreach(Transform trans in roomListContent)
{
Destroy(trans.gameObject);
}
for(int i = 0; i < roomList.Count; i++)
{
if(roomList[i].RemovedFromList)
continue;
Instantiate(roomListItemPrefab, roomListContent).GetComponent<RoomListItem>().SetUp(roomList[i]);
}
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Instantiate(PlayerListItemPrefab, playerListContent).GetComponent<PlayerListItem>().SetUp(newPlayer);
}
}
For that, In lobby rpc or sync call do not works, you need an event call that will let all other players in room which scene to be play.
– Mohammed-Rehman