[BUG] IndexOutOfRangeException in ResourceManagerConfig.CreateArrayResult

Trying to call ResourceManager.ProvideResources<Object[ ]> (locations) results in an IndexOutOfRangeException inside ResourceManagerConfig.CreateArrayResult().

Addressables version: 1.8.3

Here’s the perpetrator’s code:

        public static Array CreateArrayResult(Type type, Object[] allAssets)
        {
            var elementType = type.GetElementType();
            if (elementType == null)
                return null;
            int length = 0;
            foreach (var asset in allAssets)
            {
                if (asset.GetType() == elementType)
                    length++;
            }
            var array = Array.CreateInstance(elementType, length);
            int index = 0;

            foreach (var asset in allAssets)
            {
                if(elementType.IsAssignableFrom(asset.GetType()))
                    array.SetValue(asset, index++);
            }

            return array;
        }

As you can see, it first checks each element to have the exact type (which will be false obviously, because UnityEngine.Object is not used directly) to determine the array size. Then, when filling the array, it uses IsAssignableFrom which will be true. As a result, it will try to assign an element at index 0 in a 0 size array and fail with the aforementioned exception.

To fix, first loop needs to be rewritten as follows:

            foreach (var asset in allAssets)
            {
                if (elementType.IsAssignableFrom(asset.GetType()))
                    length++;
            }

Flagging for the team. If you haven’t yet, could you also file a bug report for us? Unity QA: Building quality with passion

1 Like

Created case 1243065.