Skip to content

Instantly share code, notes, and snippets.

@nothke
Last active May 27, 2020 15:31
Show Gist options
  • Select an option

  • Save nothke/30bfe14c55ba314ad714f80bb4e1bd6a to your computer and use it in GitHub Desktop.

Select an option

Save nothke/30bfe14c55ba314ad714f80bb4e1bd6a to your computer and use it in GitHub Desktop.
public static void SafeDestroy<T>(T obj) where T : Object
{
if (Application.isPlaying)
Object.Destroy(obj);
else
Object.DestroyImmediate(obj);
}
// Call in OnValidate, OnTriggerEnter, etc, with StartCoroutine() out of play mode
public static IEnumerator Catch22Destroy<T>(T obj) where T : Object
{
yield return null;
SafeDestroy(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment