Objects from previous scenes appear when a new player connects and joins.

Options
I have a problem which occurs when one or more players change scene and then another player joins. Some objects from the previous scenes are present for the late player.

So for example...
- Player1 connects, and becomes master client. P1 loads LobbyScene with PhotonNetwork.LoadLevel. P1 instantiates an avatar GameObject/PhotonView.
- Player2 connects. PhotonNetwork.automaticallySyncScene is true so Player to loads LobbyScene. P2 instantiates an avatar Gameobject/PhotonView.
- Everything is working correctly.
- GameScene is loaded and both players are synced up. P1 & P2's avatars are destroyed on the scene load. P1 & P2 instantiate new avatars. Everything still working as expected.
- Player3 connects, and loads into GameScene. P3 sees two P1 avatars and two P2 avatars.
- Return to LobbyScene. All players see one avatar for each other player. Everything seems correct.
- Now, if any player disconnects and rejoins, they will see multiple avatars for each player, equaling the number of scene loads (ie # of times player instantiated an avatar) throughout the room's lifetime.

Once players are connected, changing scenes works fine..."old" objects are destroyed with the scene and aren't seen again out of place. It's like on the initial scene load all of the Instantiate RPCs are received but none of the Destroys. I don't know enough about the message queue or the registry of PhotonViews to figure out why.

Looking for ideas, thanks.

Comments

  • [Deleted User]
    Options
    Hi @nomad,

    P1 & P2's avatars are destroyed on the scene load.


    Do they get destroyed by scene loading or do you destroy them using PhotonNetwork.Destroy(...)? If they get destroyed by scene loading the Instantiation call is still buffered on the server and furthermore gets executed on new connecting clients, as you already mentioned above. So please try using PhotonNetwork.Destroy(...) before loading the new scene. Another possibility you have is to call PhotonNetwork.DestroyPlayerObjects(...) before instantiating the new object used in the game scene. You can find more information this function here.
  • nomad
    Options
    Hi @Christian_Simon and thanks for the reply,

    I tried calling PhotonNetwork.Destroy() during the objects' OnDestroy and it looks like it's fixing the problem. The same thing was also happening with scene objects.

    I guess I was confused about how automaticallySyncScene works. I assumed because the objects were being destroyed by Unity unloading the scene that Photon knew to destroy the photonViews? Apparently I was wrong and the objects were destroyed but PhotonViews persisted. I noticed that PhotonViews which exist in the scene files get cleaned up and were not part of my problem, but scene objects instantiate explicitly were problematic. What is the difference between a PhotonView in a scene file and a PhotonView created with PhotonNetwork.InstantiateSceneObject() ?
  • maxclark
    Options
    @nomad Had the same issue and saw in the docs that "Objects loaded with a scene are ignored, no matter if they have PhotonView components."

    See https://doc-api.exitgames.com/en/pun/current/class_photon_network.html#a3b52beccf64860705cc467d7abf9fc41.