Cause and mechanism
IL2CPP converts C# code ahead of running the application. If a type or method works in the Editor but is missing in a build, distinguish missing ahead-of-time (AOT) code from stripping, which removes code considered unused. Reflection, looking up types or methods at runtime, can still work when the required code is preserved.
Check and correct the setup
- Identify the type and method in the exception; preserve reflection targets with link.xml or Preserve.
First error, references and target environment - Reference dynamically used generic combinations from a preserved method. An unused private method alone can be stripped.
First error, references and target environment - The example is for code generation. Do not call it during gameplay; substitute the actual missing combinations.
First error, references and target environment
using UnityEngine.Scripting;
public static class AotHints
{
[Preserve]
public static void UsedOnlyForAOTCodeGeneration()
{
Process<int>(0);
Process<MyData>(default);
}
private static void Process<T>(T value) where T : struct { }
}
public struct MyData { public int Id; }The empty Process<T> method is a placeholder illustrating the syntax. Adding it unchanged does not generate AOT code for unrelated library or application methods. Reference the actual failing generic method with the required type combinations, and separately preserve reflection targets.
Limits and verification
Generic sharing depends on Unity version and IL2CPP settings. Do not infer preservation needs solely from value versus reference types; exercise the affected path in the target IL2CPP build.
A focused diagnostic setup
Choose one action that works in Editor but fails in the target Player. Record the exception, type, member, actual generic arguments, Unity version and stripping configuration.
Test in this order
- First distinguish a missing reflected member from missing generated code for an invocation.
First error, references and target environment - Preserve the necessary members narrowly and inspect a retained path referencing the actual required closed generic method.
First error, references and target environment - Build a clean target Player and execute the failing path; successful Editor compilation is insufficient.
First error, references and target environment
Interpret the result
Preserving a member does not establish every AOT-generation condition. Map the example to the real failing operation rather than adding its empty Process placeholder. Change one diagnostic condition at a time.