PhotonNetwork.LoadLevel & SceneLoaded

Options
Joss51
Joss51

Hello,

I use PhotonVoice and I load scenes when players are in a room.

Elements of Voice(Recorder, Speaker..) are on my player object.

I don't want to destroy my players objects each time I load a new scene for instanting a new one because Voice stop working when I destroy the player object but don't restart when I create the new one.


So I declare DontDestroyOnload on my player objects, I use PhotonNetwork.LoadLevel to load scenes, and, with SceneManager.sceneLoaded, I detect when PhotonNetwork.LoadLevel has finished to load my new scene. Then I launch a method of a SpawnManager (in the new scene) to change position of my player object.

It works great but I have a problem :

Sometimes my player is spawned on the old scene place, as if PhotonNetwork.LoadLevel returns that the scene is loaded but the SpawnManager is the old one.

Is it a problem of cache ?

I could place my spawn positions nearly at the same place in all scenes but I can't imagine there is no solution for that.

Can you help me ?

Thanks a lot !

Answers

  • Tobias
    Options

    So the SpawnManager is always loaded with the scene?

    It could be that some scripts are sometimes running earlier than others and either your reference to the SpawnManager is not updated (yet) or something like that.

    You could work out the execution order or .. less scientifically just delay the spawning for a frame!?

  • Joss51
    Joss51
    edited December 2021
    Options

    Tobias, thanks for your feedbak and sorry for the delay.

    Each scene has its own SpawnManager with its references for players positions for starting.

    So my player gameobject can start in different positions.

    With SceneManager.sceneLoaded, each player runs a method in the SpawnManager of the loaded scene to place the player avatar to its right place.

    But sometimes, it runs as if the SpawnManager was the one of the previous scene.

    Are you sure PhotonNetwork.LoadLevel returns SceneManager.sceneLoaded exactly when the player scene is really loaded ?

    Note : my player gameobjects has a Photon Transform View component.