/// <summary> /// Removes all attached colliders from the specified game object. /// </summary> public static void RemoveAllColliders(this GameObject gameObject) { // Loop through all colliders and destroy them Collider[] all3DColliders = gameObject.GetComponents <Collider>(); foreach (Collider collider3D in all3DColliders) { // Destory collider #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(collider3D); #else RuntimeEditorApplication.Destroy(collider3D); #endif } Collider2D[] all2DColliders = gameObject.GetComponents <Collider2D>(); foreach (Collider2D collider2D in all2DColliders) { // Destory collider #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(collider2D); #else RuntimeEditorApplication.Destroy(collider2D); #endif } }
/// <summary> /// Destroys all children of the specified game object. /// </summary> public static void DestroyAllChildren(this GameObject gameObject) { // Loop through all child transforms Transform objectTransform = gameObject.transform; Transform[] allChildTransforms = gameObject.GetComponentsInChildren <Transform>(); foreach (Transform childTransform in allChildTransforms) { // Same as parent object? if (objectTransform == childTransform) { continue; } // Destroy object #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(childTransform.gameObject); #else RuntimeEditorApplication.Destroy(childTransform.gameObject); #endif } }