private static void DestroyCustomObjects() { while (SpawnedObjects.Count != 0) { GameObject gameObject = SpawnedObjects[0]; SpawnedObjects.Remove(gameObject); UnityEngine.Object.Destroy(gameObject); foreach (TubeBloomPrePassLight tubeBloomPrePassLight in gameObject.GetComponentsInChildren <TubeBloomPrePassLight>(true)) { //Unity requires this to be present, otherwise Unregister won't be called. Memory leaks may occour if this is removed. tubeBloomPrePassLight.InvokeMethod <object, BloomPrePassLight>("UnregisterLight"); } } while (SpawnedComponents.Count != 0) { Component component = SpawnedComponents[0]; SpawnedComponents.Remove(component); UnityEngine.Object.Destroy(component); } }