How Do I Load New Scenes With Pun?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How Do I Load New Scenes With Pun?

TheFrostedPickle
2022-05-20 18:03:38

I have been trying to load the next level in my multiplayer game when someone enters the end game collider but I'm not quite sure how to go about doing this. I have tried a few different ways but there always seems to be something that goes wrong, such as photon instantiating a few more players than it should or not all players being loaded into the next level. Your help is greatly appreciated!

Comments

IBX00
2022-05-21 17:55:33

@TheFrostedPickle Try - "PhotonNetwork.AutomaticallySyncScene = false;"

This will prevent the host players to load into the scene automatically which was loaded by the master client.

If I'm correct, you're trying something similar to the "Stumble Guys" game mechanism? If that is true, there are a couple of ways of doing this.

Write back if I can be any more of help.

TheFrostedPickle
2022-05-21 18:22:04

IBX00 2022-05-21T17:55:33+00:00

@TheFrostedPickle Try - "PhotonNetwork.AutomaticallySyncScene = false;"

This will prevent the host players to load into the scene automatically which was loaded by the master client.

If I'm correct, you're trying something similar to the "Stumble Guys" game mechanism? If that is true, there are a couple of ways of doing this.

Write back if I can be any more of help.

Hi thanks for the help but I am still running into issues. So I tried what you said and for some reason it only loads one of the players into the scene when that player touches the end game part of the level, but when the other player touches it they get loaded into the next level but by themselves. The first player can see that player in their level but the second player can't see the first player. Also the actual level doesn't sync up as well. I tried two different ways of doing this but both had the same results...

Here is the two different code that I tried:

The First code:

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")

{

PhotonNetwork.AutomaticallySyncScene = false;

PhotonNetwork.LoadLevel(2);

}

}

The Second Code:

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")

{

PhotonNetwork.AutomaticallySyncScene = false;

SceneManager.LoadScene(2)

}

}

Thank You!

IBX00
2022-05-21 19:39:20

@TheFrostedPickle You actually set AutomaticallySyncScene to false for all the players when they load into the first level and then manage the scene loading via syncing the scene index (Random or fixed) via RPC calls. But in your case, it will only make things complicated.

So I'd suggest you use custom room properties.

Let's say you have 10 players and only (first 5) half of the players can cross into the next level. In this case, you have to create a custom room property to keep track of the number of players who crossed the finish line and the number of players can yet cross. Here, data is synced across all the players easily. Once the first 5 players have crossed the finish line you can kick the remaining players outside of the room and change the room to the next level.

And yes, don't forget to keep track of the master client, in case he/she didn't make it to the finish line, change the master client to any of the players who have, and then kick all the remaining (losers) outside the room. Now, you can easily manage to change rooms among certain players. This way you can also load all the players in the next scene at once.

I guess the game manager will have to do the heavy lifting of all the logic for this.

I've not worked on this mechanic so I can't exactly tell you what will work but this is what comes to my mind for this.

Happy to help. Let me know if this works or if you have a better solution.

TheFrostedPickle
2022-05-21 21:10:12

IBX00 2022-05-21T19:39:20+00:00

@TheFrostedPickle You actually set AutomaticallySyncScene to false for all the players when they load into the first level and then manage the scene loading via syncing the scene index (Random or fixed) via RPC calls. But in your case, it will only make things complicated.

So I'd suggest you use custom room properties.

Let's say you have 10 players and only (first 5) half of the players can cross into the next level. In this case, you have to create a custom room property to keep track of the number of players who crossed the finish line and the number of players can yet cross. Here, data is synced across all the players easily. Once the first 5 players have crossed the finish line you can kick the remaining players outside of the room and change the room to the next level.

And yes, don't forget to keep track of the master client, in case he/she didn't make it to the finish line, change the master client to any of the players who have, and then kick all the remaining (losers) outside the room. Now, you can easily manage to change rooms among certain players. This way you can also load all the players in the next scene at once.

I guess the game manager will have to do the heavy lifting of all the logic for this.

I've not worked on this mechanic so I can't exactly tell you what will work but this is what comes to my mind for this.

Happy to help. Let me know if this works or if you have a better solution.

Do you know any good resources on how to do this? I'm still pretty new to Pun.

IBX00
2022-05-22 04:45:59

@TheFrostedPickle Photon Networking 2 - YouTube: This tutorial is not much but should help.

Build Multiplayer Games With Unity And Photon ( PUN 2) | Udemy - This course has 3 main sections, the third section is about a racing game. The game is almost useless but the tutorial covers many basic topics including Custom Room Properties and simple matchmaking. You can directly switch to the 3rd section.

The small games provided with photon plugin will help you as well.

Back to top