Regarding `InstantiateAsync`, `UnsafeUtility.As` was used to forcibly convert the `Object[]`, Causing IL2Cpp runtime errors

After instantiating a set of objects using UnityEngine.Object.InstantiateAsync, Unity defaults to returning UnityEngine.Object[], which are then forcibly converted to T[] using Unity.Collections.LowLevel.Unsafe.UnsafeUtility.As in UnityEngine.AsyncInstantiateOperation<T>

In this process, the underlying C # objects are still UnityEngine.Object[], and the mechanism of IL2Cpp did not collect the corresponding metadata. After compilation, it may result in incorrect use of the IEnumerable<T>.GetEnumerator method.

If you use the Indexer to method array objects normally, there is no problem (the same goes for direct FHIR)

But once the IEnumerable<T>.GetEnumerator method (such as Linq) is used, an error will occur.

Here is the test code(My engine version is 6000.0.17f1)

using System;
using System.Linq;
using UnityEngine;

public class Test : MonoBehaviour
{
    [SerializeField] private GameObject prefab;

    private async Awaitable Start()
    {
        try
        {
            await ArrayIndexer();
        }
        catch (Exception e)
        {
            print($"{nameof(ArrayIndexer)} failed!");
            Debug.LogException(e);
        }

        try
        {
            await Linq();
        }
        catch (Exception e)
        {
            print($"{nameof(Linq)} failed!");
            Debug.LogException(e);
        }

        try
        {
            await ArrayForeach();
        }
        catch (Exception e)
        {
            print($"{nameof(ArrayForeach)} failed!");
            Debug.LogException(e);
        }

        try
        {
            await LinqForeach();
        }
        catch (Exception e)
        {
            print($"{nameof(LinqForeach)} failed!");
            Debug.LogException(e);
        }
    }

    private async Awaitable ArrayIndexer()
    {
        var go = await InstantiateAsync(prefab);
        print(go[0].name); // Success
    }

    private async Awaitable Linq()
    {
        var go = await InstantiateAsync(prefab);
        print(go.First()); // Failure
    }

    private async Awaitable ArrayForeach()
    {
        var gos = await InstantiateAsync(prefab, 10);
        foreach (var go in gos) print(go.name); // Success
    }

    private async Awaitable LinqForeach()
    {
        var gos = await InstantiateAsync(prefab, 10);
        foreach (var go in gos.Where(_ => true)) print(go.name); // Failure
    }
}


Let me add some information, the bug only appears after the IL2Cpp is built. I have selected Minimal cutting mode for my IL2Cpp

@Yuze_75 Your analysis looks to be correct - the conversion from UnityEngine.Object[] to T[] using Unsafe.As does cause this problem for IL2CPP. Would you please report your test code as a bug using the bug reporter so we can address it?