Reference to Main Bridge lost after scene transition back to starting scene

We have a menuScene where the users can connect to a multiplayer room. After connecting to the server the players starts the game by loading the game world scene. (using SceneManager.LoadSceneAsync)

We have our own gameobject MultiplayerManager with DontDestroyOnLoad set that has a reference to the bridge in the starting menuScene, this game object contains following code for Awake and OnSceneLoaded:

void Awake()

  • {*

  •    if (instance == null)*
  •    {*
  •        DontDestroyOnLoad(this.gameObject);*
  •        instance = this;*
  •    }*
  •    else*
  •    {*
  •        Destroy(gameObject);*
  •    }*
  •    // Move the bridge to DontDestroyOnLoad but still*
  •    // instantiate into the active scene*
  •    var scene = bridge.gameObject.scene;*
  •    DontDestroyOnLoad(bridge);*
  •    bridge.InstantiationScene = scene;*
  •    // Make the query find the bridge*
  •    query.BridgeResolve += _ => bridge;*
  •    // Make new CoherenceSync:s find the bridge*
  •    CoherenceSync.BridgeResolve += _ => bridge;*
  •    // Get notified if the scene is changed*
  •    SceneManager.sceneLoaded += OnSceneLoaded;*
  •    *
  • }*

  • void OnSceneLoaded(Scene scene, LoadSceneMode mode)*

  • {*

  •    *
  •    // Moves the client connection to another scene on the server*
  •    bridge.SceneManager.SetClientScene(scene.buildIndex);*
  •    // Instantiate remote entities into the new scene instead*
  •    bridge.InstantiationScene = scene;*
  • }*

Loading the game world scene seems to be working fine, but then when we try to go back to the menu Scene again we get the following null pointer exception after loading in the menu scene:

NullReferenceException: Object reference not set to an instance of an object
Coherence.Toolkit.CoherenceQuery.Start () (at Library/PackageCache/io.coherence.sdk@1.1.3/Coherence.Toolkit/CoherenceQuery.cs:41)

line 41 there looks like this
bridge.OnAfterFloatingOriginShifted += OnFloatingOriginShiftedInternal;

We also get a nullpointer exception from our MultiplayerManager object when it tries to use the reference to the main query on this line in the OnSceneLoaded function:

Best Regards

Hello Simon!

My preferred way of doing this is setting the Main Bridge flag in the coherenceBridge inspector to true, this will make coherence set it as DontDestroyOnLoad for you, and the bridge can be referenced in your scripts via CoherenceBridgeStore.MasterBridge.

Then to handle setting the Client Scene and the InstantiationScene you can use a helper MonoBehaviour that you can add to your networked scenes such as:

public class InstantiationSceneSetter : MonoBehaviour
    private void Awake()
        if (CoherenceBridgeStore.MasterBridge == null)
        CoherenceBridgeStore.MasterBridge.InstantiationScene = gameObject.scene;

The DefaultExecutionOrder will make sure this script runs before any of the coherence related Awakes and OnEnables where bridge resolution happens.

Let me know how it goes!

Thanks! This solved the null pointer exceptions!

I removed the bridge and query and sync code from our MultiplayerManager awake function and removed the OnSceneLoaded and replaced it with a new class according to your suggestion and added that new class to all scenes.

I guess I should not add any CoherenceBridge to any of the other scenes since I have one marked as main in the menuScene, right?

One last thing, perhaps would be good to replace the example located last on this page with the solution you provided in this thread instead:

I guess I should not add any CoherenceBridge to any of the other scenes since I have one marked as main in the menuScene, right?

That’s correct

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.