InvalidOperationException: system state is not initialized or has already been destroyed. How to fix?

I suddenly got this error when I play the game on the editor on the second time. The first play mode works fine, but after that I couldn’t play the game anymore because of this error unless I restart the editor. It’s very annoying.

I checked the code and the error is caused by this method in ComponentSystemBase:

internal SystemState* CheckedState()
{
    var state = m_StatePtr;
    if (state == null)
    {
        throw new InvalidOperationException("system state is not initialized or has already been destroyed");
    }
    return state;
}

I don’t know where it is assigned other than it is set to null in OnAfterDestroyInternal().

If you’re not doing domain reload, it’s probably some static state (yours or possibly some backend stuff) holding onto a stale system from a world that’s been destroyed.

The full stack trace would be useful. Version info for editor and packages always helps. We can’t make bricks without clay.

1 Like

I got it. There’s indeed one static that I forgot to reset.