Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Created August 23, 2017 01:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbroadway/f3c4f0cbcebea6258694831023e807dc to your computer and use it in GitHub Desktop.
Save jbroadway/f3c4f0cbcebea6258694831023e807dc to your computer and use it in GitHub Desktop.
Unity3D editor script to remove a component from all children of an object

Unity3D editor script to remove a component from all children of an object

Usage:

  1. Save the file to your Assets/Editor folder.
  2. Modify the classes to refer to the ComponentName you want removed.
  3. Attach RemoveComponentName component to the root GameObject the components should be removed from.
  4. Click the "Remove ComponentName" button in the inspector.
  5. Remove the RemoveComponentName component when you're done.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RemoveComponentName : MonoBehaviour {
public void RemoveComponents () {
Component[] components = GetComponentsInChildren (typeof (ComponentName), true);
foreach (var c in components) {
DestroyImmediate (c);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof (RemoveComponentName))]
public class RemoveComponentNameEditor : Editor {
public override void OnInspectorGUI () {
DrawDefaultInspector ();
RemoveComponentName rmc = (RemoveComponentName) target;
if (GUILayout.Button ("Remove ComponentName")) {
rmc.RemoveComponents ();
}
}
}
@heathykeithy
Copy link

Thank you, very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment