Search code examples
unity-game-enginearkitazure-spatial-anchors

SpatialAnchors can't find AR anchor on gameobject


I'm trying to implement my own iOS project with the spatial anchors API and the provided examples on their websites. I'm following the examples very closely but for some reason my app crashes when I try to save the anchor. Xcode shows me "Didn't find AR anchor on gameobject", after that an InvalidOperationException exception occures.

I already tried debugging and found out that SessionGetArAnchorPointerForId return InPtr.Zero, which gets interpreted as a missing AR Anchor on the given GameObject. The error occures when I try to get the native anchor pointer of the gameobject.

public async void SaveAnchorInCloud(GameObject localAnchor)
   {
       localAnchor.AddARAnchor();//local Anchor is just a Cube instance
       Debug.Log("Added AR Anchor to local Anchor");
       CloudSpatialAnchor cloudAnchor = new CloudSpatialAnchor();
       cloudAnchor.LocalAnchor = localAnchor.GetNativeAnchorPointer(); //error occures here
       await this.cloudSession.CreateAnchorAsync(cloudAnchor);
       Debug.Log("Cloud Anchor erstellt: " + cloudAnchor.Identifier);
   }

I already compared with the spatial anchors sample project but I cant seem to find any difference on how the cloud anchor is saved.


Solution

  • @Chris, The reason you hit this issue is in iOS Unity for anchor creation after you create the local anchor you need to let the frame continue and use it after. If you look at our sample there is time passing before we get the native pointer. If you dispatch the code after creating the anchor this ensures one frame passes and you will not see this problem.