This is my first attempt at a custom editor and my Google-fu failed me. I was wondering what’s wrong with my code. My objective is to assign a GameObject via the Inspector then click to remove the Collider components from all of its children (rather than the laborious task of clicking each one and deleting the Collider individually). It’s a modification from the tutorial Build Object script.
----Editor-----
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(ComponentDestroyerScript))]
public class ComponentDestroyerEditor : Editor {
public override void OnInspectorGUI () {
DrawDefaultInspector();
ComponentDestroyerScript myScript = (ComponentDestroyerScript)target;
if (GUILayout.Button("Destroy Colliders")) myScript.DestroyColliders();
}
}
-------Script-------
using UnityEngine;
using System.Collections;
public class ComponentDestroyerScript : MonoBehaviour {
public Transform myObject;
private var childCollider;
public void DestroyColliders() {
foreach (Transform child in myObject) {
childCollider = child.GetComponentsInChildren<Collider>();
DestroyImmediate(childCollider);
}
}
}