Player spawns before the main scene

hello everyone

I'm having an issue with the PhotonNetwork.Instantiate function. I created a GameObject called "SpawnPlayers", and when the scene starts, it spawns a player's prefab in a random position.

The player object has private variables that when start is called, they reference with a GameObject of the scene.

When I join a room with one player using a scene called "Lobby", nothing happens and the player usually spawns, without the problems with the private variables (no errors or warnings are printed). But, when I put a second player in the same room, the function Instantiate starts before the scene, all private variables lose references and the second player can't see the first player, but the first player can see the second.

Detail: I didn't create a GameObject that spawns players at the "Lobby" scene.

I guess this isn't a problem with the private variables, because I tested the script without them, and still not working.

Why this problem is happening?

Answers

  • Hey guy, you can try to:

    Invoke("SpawnPlayers",0.2f);

    /*This is the easiest solution i think, to wait time for correctly spawns*/

  • Calling Invoke with a time delay .. is not the safest solution.

    The thing with loading scenes is that existing Game Objects get destroyed. Any new, loaded objects will get a call to Awake and Start, as soon as the scene is loaded.

    This is why PUN has a "Automatically Sync Scene" option. This is likely what you need.

    See: https://doc.photonengine.com/en-US/pun/current/gameplay/rpcsandraiseevent/