VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

Destroy LifetimeScope in Editor

Open Nitero opened this issue 2 months ago • 0 comments

I was writing an editor test where my sut calls Dispose on a LifetimeScope, which throws because it uses Destroy instead of DestroyImmediate.

This small override seems to fix it:

public class EditorSafeLifetimeScope : LifetimeScope
{
    public new void Dispose()
    {
        DisposeCore();
        if (this != null)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                DestroyImmediate(gameObject);
                return;
            }
#endif
            Destroy(gameObject);
        }
    }
}

Nitero avatar Oct 22 '25 19:10 Nitero