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
1
Question by jrat · Sep 19, 2015 at 12:15 AM · c#transformchildhierarchychildren

transform.childCount == 0, but a child shows up in the hierarchy. How is this possible?

I got an error when trying to call transform.GetChild(0). As I debugged the problem, I found out that transform.childCount was 0, so the call was out of bounds. However, I paused the game as soon as I detected that childCount was zero, and under the hierarchy it showed that the object did have a child, as shown below:alt text

Is there any way this can happen?

In case it helps, this is the C# code snippet I used to debug the program:

 if (transform.childCount == 0)
 {
     Debug.Log(transform.GetInstanceID());
     UnityEditor.EditorApplication.isPaused = true;
     return;
 }

I used the instance ID to verify that the transform of the GameObject highlighted in the hierarchy was the one with childCount == 0.

This makes no sense... please help! Thanks!

hierarchy-bug.png (3.1 kB)
Comment
Add comment · Show 2
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 RudyTheDev · Sep 19, 2015 at 02:46 PM 0
Share

Just a guess, are there any HideFlags on the child?

avatar image jrat RudyTheDev · Sep 19, 2015 at 03:00 PM 0
Share

I've never used HideFlags, so unless it's built in, no.

2 Replies

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

Answer by jrat · Dec 26, 2015 at 04:45 PM

I finally figured it out. I was instantiating the WaitBlock, assuming it would create its children in its Start method immediately after. However, I called GetChild before Start was called. What is still confusing is that even though I paused the game immediately, Start was still called afterward. Anyway, it's working now. Thanks for everyone's suggestions!

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 hexagonius · Sep 19, 2015 at 09:42 PM

by their name it's say they're runtime instantiations so they didn't come in at the same time, right? possible you're asking for child count before it exists?

Comment
Add comment · Show 4 · 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 jrat · Sep 19, 2015 at 10:00 PM 0
Share

No, I am 100% sure the child is instantiated and assigned its parent at the start of the scene, and then childCount is requested in a helper method called Initialize, called at the very end of the Start method.

avatar image hexagonius jrat · Sep 19, 2015 at 10:29 PM 0
Share

this helper method is in a script on WaitBlock(Clone) ?

avatar image jrat hexagonius · Sep 19, 2015 at 11:04 PM 0
Share

No, sorry for the confusion. I have an empty GameObject managing the scene. Both the Start and Initialize methods I referred to are called in the managing script. Initialize then calls a script a attached to the parent, WaitBlock(Clone), which asks for it's childCount.

Show more comments

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

28 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

Related Questions

Why do we access children through an object's transform? 2 Answers

Getting a tag from a child 1 Answer

Switch for children not working 1 Answer

transform.Find always returns null 1 Answer

First child of a gameobject 3 Answers


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