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
}
}

