Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Apr 23, 2016 at 08:53 PM · destroyunityeditoreditor-scriptingdestroyimmediate

Removing only colliders in a game object and its children

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);
         }
     }    
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Soraphis · Apr 23, 2016 at 09:23 PM

GetComponentsInChildren will return a list of components. you have to iterate through it to get the collider components

  public void DestroyColliders() {
      foreach (Transform child in myObject) {
          childCollider = child.GetComponentsInChildren<Collider>();
          foreach (collider in childCollider) {
              DestroyImmediate(collider);
          }
      }
  }   
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 24, 2016 at 08:23 AM 1
Share

Hi, thanks. Your code didn't quite work by its own (I needed childCollider to be in an array and collider is supposed to be preceded by Collider), but it gave me some insight into fixing the code properly.

 public Transform myObject;
 Collider[] childColliders;
 
 public void DestroyColliders() {
     childColliders = myObject.GetComponentsInChildren<Collider>();
     foreach (Collider collider in childColliders) {
         DestroyImmediate(collider);
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Toggle bool reset after using DestroyImmediate (On Editor Script) 0 Answers

I give up. My Foldout creates a very small space when expanded. How do I get rid of it? 0 Answers

How to use OnWIllDeleteAsset - Not being called 2 Answers

(Editor) How to delete duplicate components from GameObject that persists when the scene reloads 0 Answers

in editor object placement by script in editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges