Skip to content

Instantly share code, notes, and snippets.

@Madgvox
Last active March 6, 2022 06:31
Show Gist options
  • Save Madgvox/5aa8b8e77525ac93dec5e12bee662188 to your computer and use it in GitHub Desktop.
Save Madgvox/5aa8b8e77525ac93dec5e12bee662188 to your computer and use it in GitHub Desktop.
Unity: Expose an isQuitting variable to use when spawning things in OnDestroy
public static class GameStateUtility {
public static bool isQuitting { get; private set; }
[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.SubsystemRegistration )]
static void Init () {
isQuitting = false;
Application.quitting += () => {
isQuitting = true;
};
}
}
public class MyBehaviour : MonoBehaviour {
void OnDestroy () {
if( !GameStateUtility.isQuitting ) {
// spawn your objects.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment