Quick answer: IL2CPP compiles everything before the game ships and cannot generate code on the device. This exception means your game called a generic type or method with a value-type argument (a struct, enum or primitive) that the compiler never saw in your code, usually because a JSON library or other reflection created it. Reference that exact instantiation in an unused “AOT hint” method, or use Unity 2022.1 or later, where IL2CPP always emits a shared fallback.

Everything works in the editor and in a Mono build. Then the iOS, WebGL, console or Android IL2CPP build throws the first time it loads a save file or dispatches an event, with a message full of backticks and square brackets. The editor can compile a missing method on the spot. IL2CPP can only run what was compiled at build time, and it only compiles what it can find by reading your code. Below: how to read the message, what triggers it, the fix for each trigger, what changed in Unity 2021.2 and 2022.1, and how to tell it apart from a code stripping problem.

The exact error

ExecutionEngineException: Attempting to call method 'System.Collections.Generic.List`1[[SaveSlot, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.ctor' for which no ahead of time (AOT) code was generated.

ExecutionEngineException: Attempting to call method 'Test::OnMessage<Test+AnyEnum>' for which no ahead of time (AOT) code was generated.  Consider increasing the --generic-virtual-method-iterations=1 argument

The second form, with the hint about --generic-virtual-method-iterations, is what Unity 2021.2 and 2021.3 print for generic virtual methods. Very old iOS builds on Mono’s full AOT mode printed a different sentence for the same problem: Attempting to JIT compile method '...' while running with --aot-only.

It is a runtime exception and it only exists on the IL2CPP scripting backend: iOS, tvOS, WebGL and consoles always, Android when IL2CPP is selected (which ARM64 requires), and desktop when you choose IL2CPP. The editor and Mono players have a JIT compiler and never throw it.

Why IL2CPP has no code for the method

A generic type is a template. List<string> and List<Texture> can share one compiled body, because every reference type is a pointer of the same size. List<int>, List<Vector3> and List<SaveSlot> cannot: each value type has its own size and layout, so each needs its own machine code.

IL2CPP decides which of those bodies to generate by reading your compiled assemblies. If new List<SaveSlot>() appears somewhere in code, the body gets generated. If the only thing that ever builds a List<SaveSlot> is a serializer calling Activator.CreateInstance on a System.Type it worked out at runtime, the compiler has nothing to see. On a JIT runtime the missing body is compiled on demand. Under AOT there is nothing to run, and the runtime throws.

What triggers it

  1. A JSON library filling generic collections of value types. Newtonsoft Json.NET deserializing a class with a List<SaveSlot> or Dictionary<ItemKind, SaveSlot> field, where SaveSlot is a struct and ItemKind an enum, and your own code never constructs those collections. This is the classic report.
  2. MakeGenericType and MakeGenericMethod. Dependency injection containers, event buses, save systems and other serializers that close a generic over a Type found at runtime.
  3. Generic virtual methods called through an interface or base class. A visitor or handler such as IVisitor.Visit<T>(T value) called with an enum or struct. The compiler cannot always tell which implementation will receive the call, so it cannot know which body to generate.
  4. LINQ over value types. LINQ’s iterators call generic virtual methods internally, so a chain such as Where(...).Select(...) over a sequence of structs could need a combination the compiler did not produce.
  5. Tools with their own AOT step. Unity Visual Scripting generates AOT stubs before a build. A node it missed fails with this same message.

In every case the generic argument is a value type. If the type in the message is a class, read the stripping section further down first.

[Serializable] public struct SaveSlot { public int level; public float playTime; }
public enum ItemKind { Potion, Key, Relic }

public class SaveFile
{
    public List<SaveSlot> slots;                    // never constructed in your own code
    public Dictionary<ItemKind, SaveSlot> pinned;   // enum key, struct value
}

// Fine in the editor and on Mono. On IL2CPP before Unity 2022.1 this can throw:
// Json.NET creates both collections through reflection.
var save = JsonConvert.DeserializeObject<SaveFile>(json);

Reading the message to find the missing instantiation

The text between the quotes is the method that has no code, in .NET’s reflection notation:

So the first message above translates to “nobody generated new List<SaveSlot>()”. Library internals show up the same way. A message naming Newtonsoft.Json.Utilities.CollectionWrapper`1[[System.Int32, ...]] means Json.NET needed its own wrapper type over int.

Fix them one at a time. The exception stops the first failing call, so the next missing instantiation only appears after the first is fixed. Expect a few rounds for a large save format.

What your Unity version changes

Unity versionBehaviourWhat to do
2021.1 and earlierOnly instantiations found at build time exist.Add AOT hints for each one.
2021.2 and 2021.3 LTSSame by default. Adds the IL2CPP Code Generation option (in Build Settings in these versions).Add hints, or set the option to Faster (smaller) builds, which turns on full generic sharing.
2022.1 and later, including 2022.3 LTS and Unity 6IL2CPP always generates a fully shared version of generic code that works for any type argument, and falls back to it.Nothing for correctness. Hints still help speed.

From 2022 the option lives in Edit > Project Settings > Player > Other Settings > Configuration > IL2CPP Code Generation. Faster runtime is the default: it generates specialised code for every instantiation the compiler can find and keeps the shared version for the ones it cannot. Faster (smaller) builds uses the shared version much more widely, which shrinks the build and shortens build times at some cost in runtime speed for generic code.

Unity’s own description of the 2022.1 change is that this exception no longer appears for missing generic instantiations. Reports from 2022.3 and Unity 6 are rare, and usually involve a tool with its own AOT generation such as Visual Scripting. If you get one on a current version with plain C#, treat it as a possible engine bug and report it with the full message.

Fix 1: an AOT hint method

Write the instantiation in code the compiler can see. The method never runs. It only has to exist:

using System;
using System.Collections.Generic;
using UnityEngine.Scripting;

public static class AotHints
{
    // Never called. It shows IL2CPP which generic instantiations the game needs.
    [Preserve]
    static void UsedOnlyForAOTCodeGeneration()
    {
        new List<SaveSlot>();
        new Dictionary<ItemKind, SaveSlot>();
        new MessageBus().Publish<DamageEvent>(default);   // a generic method over a struct

        throw new InvalidOperationException(
            "This method is used for AOT code generation only. Do not call it at runtime.");
    }
}

The [Preserve] attribute matters. Managed code stripping runs before IL2CPP, and an unreferenced method in an unreferenced class is exactly what it removes. Without the attribute the hint can disappear before the compiler reads it.

Use the exact types from the message. List<int> does not help List<SaveSlot>.

Fix 2: Json.NET’s AotHelper

Json.NET builds for Unity (the Newtonsoft.Json-for-Unity fork, and Unity’s com.unity.nuget.newtonsoft-json package, which is built from it in version 3) include a helper that references the collection and the internal wrapper types the library needs:

using Newtonsoft.Json.Utilities;
using UnityEngine;

public class JsonAotHints : MonoBehaviour
{
    void Awake()
    {
        AotHelper.EnsureList<SaveSlot>();
        AotHelper.EnsureDictionary<ItemKind, SaveSlot>();
    }
}

The script does not need to be in a scene. Deriving from MonoBehaviour is only there so the class is compiled and kept. AotHelper.EnsureType<T>() does a related job for stripping: it keeps a type such as a custom converter from being removed. If AotHelper does not resolve in your project, your Json.NET build does not ship it, and a plain hint method does the same work.

The cheaper design fix is to keep value types out of reflective paths. A save format made of classes, with int in place of enum dictionary keys, never needs a hint on any Unity version.

Fix 3: generic virtual methods

public interface IVisitor { void Visit<T>(T value); }

void Send(IVisitor visitor) => visitor.Visit(Team.Red);   // which class's Visit<Team>?

The hint has to name the concrete implementation: new DamageVisitor().Visit<Team>(default); for each implementing class and each value type you pass. On 2021.2 and 2021.3 you can also let IL2CPP search further, as the message suggests, by raising the iteration count from an editor script:

PlayerSettings.SetAdditionalIl2CppArgs("--generic-virtual-method-iterations=2");

Higher values lengthen the build and add code that may never run, so prefer explicit hints for a handful of cases. Where you control the design, a non-generic overload per type (Visit(Team value)) avoids the problem.

Fix 4: full generic sharing, or a newer Unity

On 2021.3, switching IL2CPP Code Generation to Faster (smaller) builds enables full generic sharing, and Unity staff have suggested it on the forums as the way to stop this exception without upgrading. Measure afterwards: shared generic code cannot assume a size or layout for its type argument, so tight loops over generic collections of structs get slower.

On 2022.1 and later the fallback is always there, whichever option you pick. That changes what hints are for. They no longer prevent a crash. They let IL2CPP emit the fast specialised body for hot paths that would otherwise run on the slower shared one.

Not the same as code stripping

Managed code stripping removes types and members nothing appears to reference. When reflection later looks for them you get a different failure: MissingMethodException (often “Default constructor not found for type ...”), a TypeLoadException, or a null from Type.GetType. Those are fixed with a link.xml file or [Preserve], and they affect classes as much as structs. See IL2CPP stripping: method not found and stripping removes needed types.

The AOT error is the opposite situation. The generic definition exists and was kept. What is missing is one compiled version of it. A link.xml entry cannot create that, and lowering the stripping level does not help.

How to confirm the fix

You do not need a phone for each attempt. Add the IL2CPP module for your desktop platform, set Scripting Backend to IL2CPP for Standalone, and build. Desktop IL2CPP follows the same AOT rules, with a far shorter loop than an iOS build through Xcode. Then:

Catching the ones you missed

These errors hide in rarely used paths: an old save version, an event only one boss raises. Because it is a managed exception in a running game, Bugnet’s Unity SDK files it from iOS, Android and WebGL builds with the stack trace, recent log lines, platform, OS version and game version. The message names the exact method, and reports with the same title stack into one entry, so each missing instantiation becomes one bug with an occurrence count and the hint to write is in its title.

One limit: the SDK sees what reaches Unity’s log. A serializer wrapped in a try/catch that swallows the exception leaves nothing to report, so log it with Debug.LogException in the handler. Setup is in the getting started guide.

Frequently asked questions

What does “no ahead of time (AOT) code was generated” mean in Unity?

IL2CPP compiles all code at build time and cannot compile more on the device. The message means the game called a generic type or method with a value-type argument that the compiler never saw in your code, typically because reflection or a serializer created it at runtime.

Why does it work in the Unity editor but fail on iOS, WebGL or Android?

The editor and Mono players include a JIT compiler that produces missing generic code on demand. IL2CPP platforms have no JIT, so a generic instantiation that was not generated during the build has no code to run.

How do I fix it for Newtonsoft Json.NET?

Reference the missing collection types so IL2CPP generates them. Call AotHelper.EnsureList and AotHelper.EnsureDictionary from Newtonsoft.Json.Utilities with the struct or enum types from the error message, or construct the same generic collections in an unused method marked with the Preserve attribute.

Does Unity 2022 or Unity 6 still have this error?

From Unity 2022.1, IL2CPP always generates a fully shared version of generic code that works for any type argument and falls back to it, so missing instantiations no longer throw. Remaining reports are rare and usually involve tools with their own AOT step, such as Visual Scripting.

Is this the same as a managed code stripping problem?

No. Stripping removes whole types or members, which shows up as MissingMethodException or a null type and is fixed with link.xml or the Preserve attribute. The AOT error means the generic code exists but one value-type version of it was never compiled.

The type inside the double square brackets of the message is the hint you need to write. Copy it exactly, and keep the hint method alive with the Preserve attribute.