Pun Not Loading Scene For All Players
I am making a multiplayer game using pun and I am trying to load the player from the first level to the second, but for some reason it isn't working. It either only loads one of the clients to the next level and not the the other, or it will load both but not in the same scene and only one of the clients will be able to see the other or it will work fine. Also when you finish the second level it is supposed to bring you back to the first level but it doesn't and it usually just makes the players not be in the same scene. It's weird, I know, this is my first time coding in Pun. Sorry if this question wasn't written well as this is my first time doing this. Here is my code:
For going to the first level to the second level:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class StartGame : MonoBehaviour
{
private void Awake()
{
}
private void OnTriggerEnter(Collider other)
{
// Later I will have it load a random.range so it picks a random scene out of multiple gamemodes
if (other.tag == "Player")
{
if (PhotonNetwork.IsMasterClient)
{
PhotonNetwork.LoadLevel(3);
} else
{
GetComponent<PhotonView>().RPC("LoadSceneMaster", RpcTarget.All);
}
}
}
[PunRPC]
void LoadSceneMaster()
{
PhotonNetwork.LoadLevel(3);
}
}
And here is the code for going back to level 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
// Timer
private float currentTime = 0f;
private float startingTime = 60f;
public TextMeshProUGUI timerText;
// Finished With Game
private int amountOfFinishedPlayers = 0;
private int amountOfDeadPlayers = 0;
public TextMeshProUGUI finishedPlayersText;
public PlayerMovement[] players;
PhotonView view;
bool foundPlayers = false;
// Start is called before the first frame update
void Start()
{
view = GetComponent<PhotonView>();
// Reset Timer
currentTime = startingTime;
PhotonNetwork.AutomaticallySyncScene = true;
}
// Update is called once per frame
void Update()
{
if (foundPlayers == false)
{
// Find Players
view.RPC("FindPlayersRPC", RpcTarget.All);
}
#region Timer
// Set the text for the timer
timerText.text = currentTime.ToString("0");
if (currentTime <= 0)
{
timerText.text = "0";
}
// Sets timer
currentTime -= 1 * Time.deltaTime;
// Sets finished players text
finishedPlayersText.text = amountOfFinishedPlayers.ToString() + "/" + players.Length.ToString();
#endregion
foreach (PlayerMovement player in players)
{
// If the time is up than kill the player
if (currentTime <= 0 && player.hasFinishedGame == false)
{
player.TakeDamage(100);
}
}
// If the amount of finished players is equal to the amount of players than load the next game
if (amountOfFinishedPlayers >= players.Length || currentTime <= 0)
{
if (PhotonNetwork.IsMasterClient)
{
//view.RPC("LoadSceneMaster", RpcTarget.All);
PhotonNetwork.LoadLevel(2);
}
}
}
[PunRPC]
void LoadSceneMaster()
{
PhotonNetwork.LoadLevel(2);
}
#region Management Methods
void FindPlayers()
{
// Find Players
view.RPC("FindPlayers", RpcTarget.All);
}
[PunRPC]
void FindPlayersRPC()
{
players = FindObjectsOfType<PlayerMovement>();
foundPlayers = true;
}
public void AddDeadPlayers(int amount)
{
view.RPC("AddDeadPlayersRPC", RpcTarget.All, amount);
}
[PunRPC]
public void AddDeadPlayersRPC(int amount)
{
amountOfDeadPlayers += amount;
}
public void AddFinishedPlayers(int amount)
{
view.RPC("AddFinishedPlayersRPC", RpcTarget.All, amount);
}
[PunRPC]
public void AddFinishedPlayersRPC(int amount)
{
amountOfFinishedPlayers += amount;
}
public void SubtractFinishedPlayers(int amount)
{
amountOfFinishedPlayers -= amount;
}
#endregion
}
Your help is greatly appreciated!