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 G_Sacristan · Aug 24, 2011 at 08:46 PM · gameobjectdestroytagchildren

destroying children also destroys parent itself

Hi! I want to destroy gameObject children, but gameObject dont.

First i tried:

 var children =  player.GetComponentsInChildren(Transform);
 for (var c in children)
 {
     Destroy(c.gameObject);
 }

Destroyed children as i wanted, but with a little bonus - it also destroyed the parent that I dont want

Second approach:

 var children =  player.GetComponentsInChildren(Transform);
     for (var c in children)
     {
          if(c.gameObject&&c.gameObject!=player) Destroy(c.gameObject);
     }

Doesnt destroy anything at all - weird.

Third approach:

  var children =  player.GetComponentsInChildren(Transform);
         for (var c in children)
         {
              if(c.gameObject&&!c.gameObject.CompareTag("Player")) Destroy(c.gameObject);
         }

The same as third approach.

Can someone point what im doing wrong?

Maybe i am too tired and cant see the problem.

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

4 Replies

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

Answer by sven1994 · Aug 24, 2011 at 08:58 PM

Or maybe:

 public void RemoveChildren(Transform parent)
 {
     foreach(Transform child in parent)
         GameObject.Destroy(child.gameObject);
 }

(C#)

Comment
Add comment · Show 2 · 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 BerggreenDK · Aug 25, 2011 at 10:42 PM 0
Share

You cant destroy a transform, you need to use the GameObject.

avatar image sven1994 · Aug 25, 2011 at 10:44 PM 0
Share

thanks, I forgot that.

avatar image
2

Answer by BerggreenDK · Aug 24, 2011 at 08:55 PM

I do like this when removing objects:

     public void RemoveAll(string parentName)
     {
         // find parent object
         GameObject goParent = GameObject.Find(parentName);

             foreach(Transform child in goParent.transform)     
             {
                  GameObject.Destroy(child.gameObject);
             }
     }


Its C# but I think you should be able to translate it to Javascript without problems.

Comment
Add comment · Show 2 · 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 G_Sacristan · Aug 24, 2011 at 09:00 PM 0
Share

no problem - ive got expierience in both languages. Thanks for answer!

avatar image BerggreenDK · Aug 25, 2011 at 10:45 PM 0
Share

checked the marked answer, and found an error, so I ajusted my own code to be correct.

avatar image
0

Answer by yuriythebest · May 02, 2012 at 04:35 PM

Here is my revised static version

 public static void DestroyChildren(Transform someTransform)
 {
         foreach(Transform child in someTransform)     
              GameObject.Destroy(child.gameObject);
 }
Comment
Add comment · 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
0

Answer by yuriythebest · May 02, 2012 at 04:35 PM

here is my revised static version:

 public static void DestroyChildren(Transform someTransform)
 {
         foreach(Transform child in someTransform)     
              GameObject.Destroy(child.gameObject);
 }
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

destroying children of a gameobject 1 Answer

GameObject.FindWithTag doesn't work! 1 Answer

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

Wont destroy child-gameObjects 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