Skip to content

Instantly share code, notes, and snippets.

@hasanbayatme
Last active September 1, 2017 09:15
Show Gist options
  • Save hasanbayatme/8345f699fb061d4638ce4c0929e14a06 to your computer and use it in GitHub Desktop.
Save hasanbayatme/8345f699fb061d4638ce4c0929e14a06 to your computer and use it in GitHub Desktop.
Unity, Remove Game Object children in Runtime and Editor.
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