• 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 /
  • Help Room /
avatar image
0
Question by brentshermana · Dec 30, 2015 at 08:13 PM · transformchildrenchild object

Transform.childCount and Transform.GetChild behavior

I tried to write a function which deletes the object's children. I've since found another solution for that problem, but I don't understand why the method I wrote doesn't work. Here it is:

     private void destroyAllChildren() {
         while (transform.childCount > 0) {
             print("destroy!");
             Destroy(transform.GetChild(0).gameObject);
         }
     }

Running the unity editor with this function called in Start() causes the editor to crash. I believe the person who asked this question http://answers.unity3d.com/questions/1073544/transformchildcount-question.html had pretty much the same logic issue, but there wasn't any explanation as to why there was an issue.

tl;dr, why is there an issue?

Comment
Add comment · Show 1
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 Dave-Carlile · Dec 30, 2015 at 08:46 PM 0
Share

I responded to your comment on the other question - their problem had to do with using the Invoke function with a delay. Are you using Invoke?

1 Reply

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

Answer by Dave-Carlile · Dec 30, 2015 at 08:50 PM

When you Destroy a gameobject it isn't actually destroyed until later in the game loop. Quoting the documentation:

Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

So your loop is attempting to destroy the same object multiple times because it isn't actually removed from the child collection until later.

I'm surprised that you example causes the editor to crash though. I would expect a message indicating the object has already been destroyed. What happens when you pass i instead of 0 to GetChild?

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 brentshermana · Dec 30, 2015 at 09:15 PM 0
Share

Thanks for the explanation. I meant to post a previous version of the function where I use a while loop, so I modified my question to include that one.

avatar image Dave-Carlile brentshermana · Dec 30, 2015 at 09:30 PM 0
Share

Yes, that would cause an infinite loop because childCount never decreases since the object isn't destroyed until later, and later can never happen because of your infinite loop. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gameobject transform not moving when doing animation 0 Answers

Change transform on ChildObjects except one 0 Answers

Find child coordinates and pass them further? 0 Answers

Can't get Child's Child 0 Answers

Not all tracks in timeline play in the playmode. They do work in the editor tough. (Not a static object issue) 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