Getting started with Azure Spatial Anchors: CreateAzureAnchor() crash or taking a long time to load

Rhylvin Dale Tinador 111 Reputation points
2021-04-30T11:24:53.203+00:00

I have been following this tutorial:
https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/tutorials/mr-learning-asa-02

there were some necessary configurations and steps not included in the tutorial like:
* it needed an ARCameraManager error log
* it needed an ARSession error log
* it needed an ARAnchorManager error log

which I configured as I keep getting errors and fixing it along the way

and was able to deploy, test and follow the in-device tutorial:

1.) Move the cube to a different location
2.) Start Azure Session
3.) Create Azure Anchor (creates an anchor at the location of the cube).
......

but I got stuck in #3

when I tap for create Azure button, it just crash but when I check the Debug version, it didn't actually crash cause there were no actual errors, I think it just keeps loading and/or working on the current main thread because everything disappeared when I tap the create Azure Button.

I check the codes of its corresponding function and this is it:

92937-image.png

as you may notice it has an error, this is the actual script from the package I download, but all I had to do to fix this is add ".Result" after ".GetPointer()"

I divided the script and added logs so I can see which part is the problem:

92945-image.png

the very last to log is: "tastInpt" which means getting the result is the problem.

is this fixable? can anyone help me get through this?

Azure Spatial Anchors
Azure Spatial Anchors
An Azure service that is used to build immersive three-dimensional applications and experiences that map, persist, and restore content or points of interest at real-world scale.
87 questions
{count} votes

Accepted answer
  1. Rhylvin Dale Tinador 111 Reputation points
    2021-05-26T07:17:59.283+00:00

    Was able to fix this on my own by using this:
    a github repository of Azure samples. I actually don't know why this worked but the samples from Microsoft didn't. but here you go:

    https://github.com/Azure/azure-spatial-anchors-samples

    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Christian Glessner 6 Reputation points
    2021-05-26T08:40:58.35+00:00

    Hi, have exactly the same issue as described above. The Microsoft examples are running, but when in my own projects I get also the Null ref error. I can't find any difference in the code.

    1 person found this answer helpful.
    0 comments No comments

  2. Rhylvin Dale Tinador 111 Reputation points
    2021-05-05T07:40:09.147+00:00

    Created this script following the new given tutorial:
    https://learn.microsoft.com/en-us/azure/spatial-anchors/how-tos/create-locate-anchors-unity

    I still use the same project but created a difference scene, configure scene to run MR
    I just made it easier by using Coroutine instead of tap input to generate the AR Object.
    so basically when the hololens app starts the AR Object is parented to the camera in front of it, so it will move as you move your head
    we wait for 5 seconds. and then it starts the session.
    and then wait another 10 seconds(the purpose for the RecommendedForCreateProgress).
    if its ok, then we unparent the AR Object(to let it stay in place) and CreateAnchor() and then there comes the issue.
    it seems there is an issue in here, they return null reference.. why?:

                    cloudNativeAnchor.NativeToCloud();  
    

    and in here:

            CloudSpatialAnchor cloudAnchor = cloudNativeAnchor.CloudAnchor;  
    

    here are the logs:

    Start  
    value.RecommendedForCreateProgress: 2.4  
    Starting to Create Anchor  
    cloudNativeAnchor.CloudAnchor == null  
    NullRefrenceExcepetion: Object reference not set to an instance of an object  
    SetupAnchor  
    NullRefrenceExcepetion: Object reference not set to an instance of an object  
    

    here is my script:

        public GameObject ARAnchor;  
        CloudSpatialAnchorSession cloudSession;  
        string feedback = "";  
        // Start is called before the first frame update  
        void Start()  
        {  
            StartCoroutine(StartSession());  
        }  
      
        IEnumerator StartSession()  
        {  
            yield return new WaitForSeconds(5);  
            Debug.Log("Start");  
            cloudSession = new CloudSpatialAnchorSession();  
            cloudSession.Configuration.AccountKey = "xxxxx";  
            cloudSession.Configuration.AccountId = "xxxxx";  
            cloudSession.Configuration.AccountDomain = "southeastasia.mixedreality.azure.com";  
            cloudSession.Start();  
            cloudSession.SessionUpdated += (object sender, SessionUpdatedEventArgs args) =>  
            {  
                var status = args.Status;  
                if (status.UserFeedback == SessionUserFeedback.None) return;  
                feedback = $"Feedback: {Enum.GetName(typeof(SessionUserFeedback), status.UserFeedback)} -" +  
                    $" Recommend Create={status.RecommendedForCreateProgress: 0.#%}";  
                Debug.Log(feedback);  
            };  
            StartCoroutine(checkIfReadyToGenerate());  
        }  
      
        IEnumerator checkIfReadyToGenerate()  
        {  
            yield return new WaitForSeconds(10);  
            checkIfReadyToGenerateAsync();  
        }  
        async void checkIfReadyToGenerateAsync()  
        {  
            SessionStatus value = await cloudSession.GetSessionStatusAsync();  
            Debug.Log("value.RecommendedForCreateProgress: " + value.RecommendedForCreateProgress);  
            if (value.RecommendedForCreateProgress < 1.0f)  
            {  
                Debug.Log("Not Yet Ready to create Anchor");  
                StartCoroutine(checkIfReadyToGenerate());  
            }  
            else  
            {  
                Debug.Log("Starting to Create Anchor");  
                CreateAnchor();  
            }  
        }  
      
        async void CreateAnchor()  
        {  
            ARAnchor.transform.parent = null;  
            ARAnchor.AddComponent<CloudNativeAnchor>();  
            CloudNativeAnchor cloudNativeAnchor = ARAnchor.GetComponent<CloudNativeAnchor>();  
            if(ARAnchor.GetComponent<CloudNativeAnchor>() == null)  
            {  
                Debug.Log("CloudNativeAnchor == null");  
            }  
            if (cloudNativeAnchor.CloudAnchor == null)  
            {  
                Debug.Log("cloudNativeAnchor.CloudAnchor == null");  
                cloudNativeAnchor.NativeToCloud();  
                SetupAnchor(cloudNativeAnchor);  
            }  
        }  
      
        async void SetupAnchor(CloudNativeAnchor cloudNativeAnchor)  
        {  
            Debug.Log("SetupAnchor");  
            CloudSpatialAnchor cloudAnchor = cloudNativeAnchor.CloudAnchor;  
            //cloudAnchor.AppProperties[@"model-type"] = @"frame";  
            //cloudAnchor.AppProperties[@"label"] = @"my home test";  
            cloudAnchor.Expiration = DateTimeOffset.Now.AddDays(7);  
            Debug.Log("CreateAnchorAsync");  
            await cloudSession.CreateAnchorAsync(cloudAnchor);  
            feedback = ($"Created a cloud anchor with ID={cloudAnchor.Identifier}");  
            Debug.Log(feedback);  
        }  
    

    all in all. this is a new tutorial that I follow, don't know why Microsoft has a lot of tutorial about creating Azure Anchor. but none of them is working from me.. please help.

    0 comments No comments

  3. António Sérgio Azevedo 7,666 Reputation points Microsoft Employee
    2021-05-11T15:01:14.453+00:00

    Hello @Rhylvin Dale Tinador ,

    Please note there are some slight changes between version 2.7.0 and 2.9.0 of Azure Spatial Anchors SDK and the tutorials you are following are currently being updated so we differentiate function calls depending on what version you are using. I have raised awareness to docs team thanks to your thread and we are prioritizing the update to the Unity tutorials.

    Assuming you are using ASA 2.9 can you please try awaiting the call to NativeToCloud()?

    await cloudNativeAnchor.NativeToCloud();  
    

    I have also reviewed this thread with Product Team and we suggest that you use SpatialAnchorManager (and CloudNativeAnchor) in new projects instead of writing everything “from scratch”. Our samples show how to do this:

    Let me know if I was able to unblock you?
    Thank you so much for your feedback!

    Remember:

    • Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification.

  4. Rishabh Aggarwal 1 Reputation point
    2021-11-12T00:04:50.523+00:00

    Hi, was there a resolution on this?

    I am getting the exact same error

     public async void CreateAnchor()
        {
            var localAnchor = GameObject.Instantiate(this.SpherePrefab, this.PlacementDisc.transform.position,
                this.PlacementDisc.transform.rotation);
            localAnchor.AddComponent<CloudNativeAnchor>();
            StatusText.GetComponent<TextMeshPro>().text = "anchored";
            CloudNativeAnchor cloudNativeAnchor = localAnchor.GetComponent<CloudNativeAnchor>();
            StatusText.GetComponent<TextMeshPro>().text = cloudNativeAnchor.isActiveAndEnabled ? "yayy" : "noo";
            if (cloudNativeAnchor.CloudAnchor == null)
            {
                StatusText.GetComponent<TextMeshPro>().text = "anchor was null";
                await cloudNativeAnchor.NativeToCloud();
                StatusText.GetComponent<TextMeshPro>().text = "received native to cloud";
            }  
            CloudSpatialAnchor cloudAnchor = cloudNativeAnchor.CloudAnchor;
            //cloudAnchor.AppProperties[@"model-type"] = @"frame";
            cloudAnchor.AppProperties[@"label"] = @"testing123";
            await this.cloudSession.CreateAnchorAsync(cloudAnchor);
            StatusText.GetComponent<TextMeshPro>().text = "created cloud anchor " + cloudAnchor.Identifier;
            localAnchor.GetComponent<Material>().color = Color.green;
        }
    

    This is my code and this is the stack trace I get

    NullReferenceException: Object reference not set to an instance of an object.
      at Microsoft.Azure.SpatialAnchors.Unity.ARFoundation.AnchorHelpers.CreateAnchor (UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.ARFoundation.AnchorHelpers.CreateWorldAnchor (UnityEngine.Transform transform) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.ARFoundation.UnityARFoundationAnchorComponent.Awake () [0x00000] in <00000000000000000000000000000000>:0 
      at UnityEngine.GameObject.AddComponent (System.Type componentType) [0x00000] in <00000000000000000000000000000000>:0 
      at UnityEngine.GameObject.AddComponent[T] () [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorExtensions.CreateNativeAnchor (UnityEngine.GameObject gameObject) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorExtensions.FindOrCreateNativeAnchor (UnityEngine.GameObject gameObject) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor+<NativeToCloud>d__4.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
      at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor.NativeToCloud (System.Boolean useExisting) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor+<NativeToCloud>d__5.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
      at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor.NativeToCloud () [0x00000] in <00000000000000000000000000000000>:0 
      at AnchorStoreManager+<CreateAnchor>d__10.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 
      at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
      at AnchorStoreManager.CreateAnchor () [0x00000] in <00000000000000000000000000000000>:0 
      at UnityEngine.Events.InvokableCall.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
      at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Input.SpeechInputHandler.Microsoft.MixedReality.Toolkit.Input.IMixedRealitySpeechHandler.OnSpeechKeywordRecognized (Microsoft.MixedReality.Toolkit.Input.SpeechEventData eventData) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem+<>c.<.cctor>b__244_38 (Microsoft.MixedReality.Toolkit.Input.IMixedRealitySpeechHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.BaseEventSystem.HandleEvent[T] (UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] eventHandler) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem.DispatchEventToGlobalListeners[T] (Microsoft.MixedReality.Toolkit.Input.BaseInputEventData baseInputEventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] eventHandler) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem.HandleEvent[T] (UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] eventHandler) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem.RaiseSpeechCommandRecognized (Microsoft.MixedReality.Toolkit.Input.IMixedRealityInputSource source, Microsoft.MixedReality.Toolkit.Utilities.RecognitionConfidenceLevel confidence, System.TimeSpan phraseDuration, System.DateTime phraseStartTime, Microsoft.MixedReality.Toolkit.Input.SpeechCommands command) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Windows.Input.WindowsSpeechInputProvider.OnPhraseRecognized (UnityEngine.Windows.Speech.ConfidenceLevel confidence, System.TimeSpan phraseDuration, System.DateTime phraseStartTime, System.String text) [0x00000] in <00000000000000000000000000000000>:0 
      at Microsoft.MixedReality.Toolkit.Windows.Input.WindowsSpeechInputProvider.KeywordRecognizer_OnPhraseRecognized (UnityEngine.Windows.Speech.PhraseRecognizedEventArgs args) [0x00000] in <00000000000000000000000000000000>:0 
      at UnityEngine.Windows.Speech.PhraseRecognizer.InvokePhraseRecognizedEvent (System.String text, UnityEngine.Windows.Speech.ConfidenceLevel confidence, UnityEngine.Windows.Speech.SemanticMeaning[] semanticMeanings, System.Int64 phraseStartFileTime, System.Int64 phraseDurationTicks) [0x00000] in <00000000000000000000000000000000>:0 
    UnityEngine.GameObject:AddComponent(Type)
    UnityEngine.GameObject:AddComponent()
    Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorExtensions:CreateNativeAnchor(GameObject)
    Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorExtensions:FindOrCreateNativeAnchor(GameObject)
    Microsoft.Azure.SpatialAnchors.Unity.<NativeToCloud>d__4:MoveNext()
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<NativeToCloud>d__4&)
    Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor:NativeToCloud(Boolean)
    Microsoft.Azure.SpatialAnchors.Unity.<NativeToCloud>d__5:MoveNext()
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<NativeToCloud>d__5&)
    Microsoft.Azure.SpatialAnchors.Unity.CloudNativeAnchor:NativeToCloud()
    <CreateAnchor>d__10:MoveNext()
    System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<CreateAnchor>d__10&)
    AnchorStoreManager:CreateAnchor()
    UnityEngine.Events.InvokableCall:Invoke()
    UnityEngine.Events.UnityEvent:Invoke()
    Microsoft.MixedReality.Toolkit.Input.SpeechInputHandler:Microsoft.MixedReality.Toolkit.Input.IMixedRealitySpeechHandler.OnSpeechKeywordRecognized(SpeechEventData)
    Microsoft.MixedReality.Toolkit.Input.<>c:<.cctor>b__244_38(IMixedRealitySpeechHandler, BaseEventData)
    Microsoft.MixedReality.Toolkit.BaseEventSystem:HandleEvent(BaseEventData, EventFunction`1)
    Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem:DispatchEventToGlobalListeners(BaseInputEventData, EventFunction`1)
    Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem:HandleEvent(BaseEventData, EventFunction`1)
    Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem:RaiseSpeechCommandRecognized(IMixedRealityInputSource, RecognitionConfidenceLevel, TimeSpan, DateTime, SpeechCommands)
    Microsoft.MixedReality.Toolkit.Windows.Input.WindowsSpeechInputProvider:OnPhraseRecognized(ConfidenceLevel, TimeSpan, DateTime, String)
    Microsoft.MixedReality.Toolkit.Windows.Input.WindowsSpeechInputProvider:KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs)
    UnityEngine.Windows.Speech.PhraseRecognizer:InvokePhraseRecognizedEvent(String, ConfidenceLevel, SemanticMeaning[], Int64, Int64)