Unity & Azure Spatial Anchors - The SpatialAnchorManager suddenly disappears?

185 Views Asked by At

I am developing a Hololens 2 application in Unity following the Azure Spatial Anchor tutorials provided by Microsoft. Specifically, this one uses Unity, the Mixed Reality Toolkit, and OpenXR.

I have one scene where I am placing the Spatial Anchors (called "Edit Mode") and another where I am able to view the anchors that have been placed (called "Preview Mode"). Each scene has a script that allows the user to travel between scenes at the click of a button.

In both scenes, the SpatialAnchorManager exists in the same game object as the calling script, and is referenced like this:

private SpatialAnchorManager anchorManager;

void Start()
{
   anchorManager = GetComponent<SpatialAnchorManager>();
}

The SpatialAnchorManager is able to be successfully found this way, the session starts, and I can successfully move from the first scene ("Edit Mode") to the second one ("Preview Mode") using the code below.

public void moveToPreview()
{
  if(anchorManager.IsSessionStarted)
  { 
    anchorManager.DestroySession();
  }
  SceneManager.LoadScene("PreviewScene");
}

The "Preview Mode" scene has the exact same code, but it just points back to the "Edit Mode" scene; however, when I try to go back to "Edit Mode", the SpatialAnchorManager that already exists in "Preview Mode" cannot be found. (The exception I implemented is thrown.)

public void moveToEdit()
{
  if(anchorManager == null)
  {
    throw new Exception("Somehow the manager is not here.");
  }

  if(anchorManager.IsSessionStarted)
  { 
    anchorManager.DestroySession();
  }
  SceneManager.LoadScene("EditScene");
}

Does anyone know why this may be happening?

Unity 2021.3.4f1 Azure Spatial Anchors SDK v. 2.13.0

1

There are 1 best solutions below

2
Nathan - MSFT On

From reviewing above, I believe you are stating that this line is executing:

  if(anchorManager == null)
  {
    throw new Exception("Somehow the manager is not here.");
  }

What is likely to happen is each scene is getting a copy of the script which means two anchorManager variables. You should be able to see that in Unity debugger.