MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Error when destroying GameObject with WorldAnchor

Open ghost opened this issue 9 years ago • 3 comments

Hello there,

In my App I am creating GameObjects as children of another GameObject. The user is required to choose one of these GameObjects by gazing and tapping at it. When tapped a WorldAnchor is created using the WorldAnchorManager.AttachAnchor-Method.

At some point I want to delete the previously selected GameObject. I therefore call the follwoing Mehtod of the parent GameObject.

public void ClearChildren()
{
  List<GameObject> childObjects = new List<GameObjects>();
  foreach (Transform child in this.Transform) childObjects.Add(child.gameObject);
  childObjects.ForEach(child => Destroy(child));
}

A script attached to the created GameObjects contains the following code which I think should take care of deleting the attached Anchor.

private void OnDestroy()
{
  anchorManager.RemoveAnchor(gameObject);
}

But I get the following Exception when doing this:

Exception thrown: 'System.NullReferenceException' in UnityEngineProxy.dll NullReferenceException: Exception of type 'System.NullReferenceException' was thrown. at UnityEngineProxy.InternalCalls.GameObject_CUSTOM_GetComponent(Object self, Type type) at UnityEngine.GameObject.GetComponentT at HoloToolkit.Unity.WorldAnchorManager.DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo) at HoloToolkit.Unity.WorldAnchorManager.Update() at HoloToolkit.Unity.WorldAnchorManager.$Invoke16(Int64 instance, Int64* args) at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method) (Filename: Line: 0)

I think the problem is, that the GameObject is already destroyed when the WorldAnchorManager tries to delete the Anchor. Is there another way to properly delete the WorldAnchor? Or is it done automatically and therfore I don't have to do it manually?

Thanks in advance

ghost avatar May 31 '17 06:05 ghost

I would say this is a bug. The delete operation in WorldAnchorManager shouldn't need to depend on the GameObject to be able to complete the removal of the anchor from the WorldAnchorStore.

Ziugy avatar May 31 '17 15:05 Ziugy

Can you delete the anchor before you delete the object? I believe the current SDK uses the gameObject to reference the world anchor, and not sure if there is another direct way to reference the anchor in the anchor store.

MatrixInception avatar Jun 18 '17 01:06 MatrixInception

As the object itself is not aware when it is due to be deleted I don't know how. I think one could extract the anchor from the gameObject via GetComponent-Method and manually delete it. But I think the proper way should be for the anchor to be safley removed whenever the gameObject is destroyed.

Maybe there is some other Event-Methode beside OnDestroy which can be used?

ghost avatar Jun 19 '17 06:06 ghost