Last active
September 1, 2017 09:15
-
-
Save hasanbayatme/8345f699fb061d4638ce4c0929e14a06 to your computer and use it in GitHub Desktop.
Unity, Remove Game Object children in Runtime and Editor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RemoveChildren : MonoBehaviour | |
{ | |
void Start () | |
{ | |
// Remove all children at start | |
RemoveAll (); | |
} | |
public void RemoveAll () { | |
List<GameObject> children = new List<GameObject> (); | |
// Getting all children and putting them to a new list | |
for ( int i = 0; i < transform.childCount; i++ ) | |
{ | |
children.Add ( transform.GetChild (i).gameObject ); | |
} | |
// Loop through children and destroy them | |
foreach ( GameObject go in children ) | |
{ | |
if ( Application.isEditor ) | |
{ | |
GameObject.DestroyImmediate ( go ); | |
} | |
else | |
{ | |
GameObject.Destroy ( go ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment