Search Unity

Help Wanted [Photon] New players don't instantiate previously joined players

Discussion in 'Multiplayer' started by SkrenZz, Feb 7, 2021.

  1. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    23
    Hello everyone,

    I am posting about an issue I am having with my multiplayer game using Photon Networking.
    My game has three main scenes:
    1. StartScene - which is used to create games and join existing ones.
    2. Lobby - after creating/joining a game, all players are loaded into this scene, where they have to claim that they're ready. Once all players are ready, the host (master-client) can start the game.
      This scene has a GO called LobbyManager, which calls PhotonNetwork.Instantiate inside the Start() method to instantiate LobbyPlayer.
    3. Game - The scene in which the actual game takes place.
    When the player is initially connected to the servers, I set PhotonNetwork.AutomaticallySyncScene to true. Everything is working as expected, until the game is over (in the Game scene).
    Once the game is over, I set PhotonNetwork.AutomaticallySyncScene to false, and allow each individual player to return to the lobby (in order to start a new game). For some reason, when the player returns to the lobby after the game is finished, it doesn't instantiate other players that have returned to the lobby before (which it does when the player joins the room for the first time).

    I don't know if it may cause the issue, but I also destroy all PhotonViews attached to the players in the Game scene once the game is finished, because I don't want the GOs to disappear from the Game scene when they return to the lobby, so I can show a replay of the game. I also set PhotonNetwork.CurrentRoom.IsOpen to false when the host starts the game.

    Any attempt to help will be greatly appreciated, thanks a lot in advance and have a wonderful day!
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    I don't really have an explanation but maybe any of this helps:

    As long as the players are in one room (no matter the scene), networked objects can be created and destroyed. If you destroy a PhotonView, it will destroy the GO it's on, too. You should use PhotonNetwork.Instantiate and .Destroy, as those are networked / synced.
    Loading a scene will destroy objects via the engine (not using PhotonNetwork.Destroy), unless you load additive. To keep objects, you could flag them via DontDestroyOnLoad(). As long as you stay in the same room, this is legit but you might want to clean up when you leave the room.
     
  3. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    23
    Hi tobiass,

    Thanks you very much for your reply, which I am happy and thankful to say has lead me to a solution for my issue. Apparently, the LobbyPlayer GO (which is a UI object) was instantiated across all clients, but it was invisible because it was not in any canvas to players still in the game scene, and when they returned to the lobby, the GO was destroyed.
    I flagged it using DontDestroyOnLoad, and added a coroutine to set its transform's parent only when the lobby scene is loaded- which did solve the issue.

    However, all of this made me wonder, is there an option to instantiate networked GOs only when a client is in a specific scene (or a better practice to do it)? If not- I would suggest adding it, I'd imagine it would be a useful feature for many games (both for simpler functionality and improved performance, by preventing unwanted objects and their components from spawning in unwanted scenes).

    Many thanks again for your help, and have a great day!
     
    Last edited: Feb 9, 2021
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,686
    Good to read you could find a solution!

    I don't know if I get this question.
    As long as all players per room are in the same scene, this should make sure only the objects for that scene are created.
    If players can load different scenes but are in the same Photon room, yes, some will instantiate objects that are not relevant to the scene they are in. But: The remote user instantiated the objects and as you normally want to track their state .. they also get created on all other players (nevermind the scene).
     
unityunity