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 /
  • 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.

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

Iterating through children causes InvalidCastException 1 Answer

reset a GameObject parents childs to 0 if a item has changed its parent 2 Answers

how to access the Transform of a moving child object? 1 Answer

Looking through all the children doesn't work 1 Answer

Destroyng all childs of an object 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