in unity how does EditorApplication.delayCall += () => work
Assistant
In Unity, EditorApplication.delayCall is an event that you can subscribe to when you want to defer or delay execution of some code until after the current Editor update loop finishes. This can be useful when you want to schedule some work to happen , but not immediately—instead, during the next Editor update cycle, effectively allowing you to "wait" until certain things stabilize or complete.
User
if I use it at foreach will it work correctly
Assistant
If you subscribe to EditorApplication.delayCallinside a foreach loop like this:
It can lead to an unexpected result due to closure and variable capture in C#.
What is the common problem?
In C# before C# 5, the loop variable captured in the lambda is the same variable shared across all iterations, meaning by the time the delayCall executes, the loop is finished and item will be the last element (or sometimes undefined) — all your lambdas will print the same value (usually the last element).
once
How EditorApplication.delayCall += () => { ... } works:
EditorApplication.delayCall is a static event of type EditorApplication.CallbackFunction that you can add delegates to.
When you do EditorApplication.delayCall += someDelegate;, you register someDelegate to be called once on the next Editor update.
The important part: after these delegates get invoked on the next update, they are cleared automatically, meaning they run only once.
The lambda () => { ... } syntax is just using an anonymous function as a subscriber.
Practical example
using UnityEditor;
public class Example
{
[MenuItem("Examples/Delay Call")]
public static void TestDelayCall()
{
EditorApplication.delayCall += () =>
{
UnityEngine.Debug.Log("This will be printed after the current Editor operation finishes.");
};
UnityEngine.Debug.Log("This is printed immediately.");
}
}
When you run TestDelayCall(), the output will be:
This is printed immediately.
This will be printed after the current Editor operation finishes.
Why use delayCall?
To avoid issues where you might want to modify Editor state but Unity is currently in the middle of some operation that doesn't allow that (e.g., after certain callbacks, during compilation, or when a hierarchy change is occurring).
To wait until the Editor’s current series of tasks complete.
To avoid reentrancy or infinite loops from certain event handlers invoking themselves synchronously.
To defer execution of code that needs to run "soon" but not immediately.
Summary
EditorApplication.delayCall += () => { ... }; schedules the enclosed code to run once on the next Editor update.
It uses an event queue internally.
Your callback will run after the current operation, in the Editor update loop.
The callbacks clear automatically once they execute.
This is a very handy tool when extending the Unity Editor through editor scripts and custom tools.
Example Problem
If collection is { 1, 2, 3 }, you might expect the three callbacks to print 1, 2, and 3. But they all might print 3.
How to fix it?
Use a local variable inside the loop to capture the current value properly:
foreach (var item in collection)
{
var currentItem = item; // <-- create a local copy inside the loop
EditorApplication.delayCall += () =>
{
Debug.Log(currentItem); // now correctly captured
};
}
This way, each lambda captures its own copy of the currentItem, so the values printed will be what you expect.