Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.07k stars 1.15k forks source link

ArgumentOutOfRangeException iOS error #531

Closed mistergreen closed 4 years ago

mistergreen commented 4 years ago

Does any know what this error is? I get it on iOS 13.x, Unity 2019.3.x, ARfoundation 3.x It randomly pops up once in a while so it's hard report it as a bug.

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00000] in <00000000000000000000000000000000>:0 
  at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <00000000000000000000000000000000>:0 
  at ARWorldMapAddressable.OnAchorAdded (UnityEngine.XR.ARFoundation.ARAnchorsChangedEventArgs obj) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Action`1[T].Invoke (T obj) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.XR.ARFoundation.ARAnchorManager.OnTrackablesChanged (System.Collections.Generic.List`1[T] addedPoints, System.Collections.Generic.List`1[T] updatedPoints, System.Collections.Generic.List`1[T] removedPoints) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.XR.ARFoundation.ARTrackableManager`4[TSubsystem,TSubsystemDescriptor,TSessionRelativeData,TTrackable].Update () [0x00000] in <00000000000000000000000000000000>:0 

(Filename: currently not available on il2cpp Line: -1)
tdmowrer commented 4 years ago

What's an ARWorldMapAddressable? That isn't part of ARFoundation -- it looks like something has subscribed to the ARAnchorManager's anchorsChanged event and the error is in that method.

mistergreen commented 4 years ago

oh, thanks. I'm blind. ARWorldMapAddressable is my script controlling anchors. I was told to use anchor pending in dealing with anchorsChanged but I guess it's addressing an anchor that doesn't exist yet.

private void OnAchorAdded(ARAnchorsChangedEventArgs obj)
    {
        try
        {
            bool pending = obj.added[0].pending;
            //  need to figure out when adding by hand or when add on load
            if (pending == false)
            {
                if (newTank) 
                {
                    PlaceTank(placementIndicator, obj.added[0]);
                }
                else
                {
                    //load from file
                    //LoadObject(obj.added[0].trackableId);
                    for(int i=0; i < obj.added.Count; i++)
                    {
                        LoadObject(obj.added[i]);
                    }
                }
            }
        }
        catch (IOException error)
        {
            Debug.Log("+++ error: " + error.Message);
        }
    }
tdmowrer commented 4 years ago

The ARAnchor and associated GameObject exist immediately. The pending flag just determines whether or not the underlying AR framework has actually picked it up and started tracking it.

Your OnAnchorAdded appears to subscribe to anchorsChanged, which will be invoked whenever

  1. A new anchor is added
  2. An existing anchor is updated
  3. An existing anchor is removed

So it's quite possible that an existing anchor got updated, and you tried to access element 0 of obj.added, which would have no elements in it, hence the ArgumentOutOfRangeException.

mistergreen commented 4 years ago

Thanks. I'll check obj.added.Count > 0 before accessing.

tdmowrer commented 4 years ago

Cool. Can we close this issue?