IS THIS A BUG? Unity calls OnDestroy on Game Objects when entering or leaving maximized window

I have a MonoBehaviour Script with OnDestroy implemented, with a simple Debug log
When running the game from the editor, whenever I switch from a normal Game window to the maximized, or from maximized to normal, the debug log is printed in the console, and any other code in OnDestroy is also executed, however, the game object was never destroyed.
Is this a bug, or known, I have not found documentation on it.
It’s making debugging pretty frustrating as OnDestroy is meant for when things are actually being destroyed, so code is executing when it shouldn’t be.

Thanks,

Unity 2022.3.9f1 (LTS) doesn’t do this for me.

That seems odd, but I wouldn’t be surprised honestly.

Historically OnDestroy() has been a troubled function, often requiring silly shims like public static bool applicationIsExiting; and annoyingly hairy code interlocks to prevent fiddling with things that might new up GameObjects such as singletons and whatnot.

Instead, try and move stuff out of Awake / OnDestroy and instead put it in OnEnable / OnDisable.

The OnEnable / OnDisable lifecycle pair is FAR more robust and reliable.

Got it, I’m on 2021.3.13 and I’ve noticed this happening for a long time, maybe it’s finally fixed.

That’s fair, I’ll check if OnDisable has similar bazar behavior.