Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Blizzard_jedi · Jun 19, 2013 at 10:23 PM · transformprefabchildren

GetComponentsInChildren problem

Good day, commUnity! I have a prefab of a character like this:

prefab

Say, I have an object iCharacter, instantiated from peasant prefab. I need to get the TargetLookAt child object from code. I tried the following ways:

1)

         Transform targetLookAt = iCharacter.transform.FindChild("TargetLookAt");
         if (targetLookAt != null)
             _targetLookAt = targetLookAt.transform;

FindChild always returns null. It seems like it searches only among the first generation of children - if it is so, then ok, this code is incorrect.

2)

         Transform [] children = iCharacter.GetComponentsInChildren <Transform> ();
         foreach (Transform child in children)
         {
             if (child.name == "TargetLookAt")
             {
                 _targetLookAt = child;
                 break;
             }
         }

This code seems to be correct. Moreover, GetComponentsInChildren should search for components in the whole hierarchy of a parent. So I expected to get all the transforms, parented to peasant. But instead I got only one Transform in the children array.

3) Ok, Transform is IEnumerable, so I can simply take the iCharacter.transform and look through all the child transforms in a loop:

         for (int i = 0; i < iCharacter.transform.childCount; ++i)
         {
             var child = iCharacter.transform.GetChild (i);
             if (child.name == "TargetLookAt")
             {
                 _targetLookAt = child;
                 break;
             }
         }

Still no result, cause iCharacter.transform.childCount = 0 somehow. So there are two questions: - How to find a specific child in a GameObject, instantiated from a prefab? - Why all my code samples don't work?

P.S. I have a sense, that all these problems have one common solution.

peasant prefab.jpg (70.9 kB)
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 Pille5 · Dec 26, 2015 at 05:40 PM 0
Share

I have the same problem. I have two game scenes and this works on one and does not work on the other. I can't see whats the difference between them.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OmarAlhaddad · Jun 19, 2013 at 11:02 PM

If you wanna do FindChild(), you gotta go through the whole hierarchy like so: FindChild("globalMove01/joints01/..etc../TargetLookAt")

What i would do though, if the child you want is part of the prefab, you can just create a public transform variable and drag it to it, away from all this nonsense!

Edit: wanted to explain why the rest of your code is not working! its because the root has only one child like you mentioned, and that child has another child which has another child (but to the root there is only one).

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 Blizzard_jedi · Jun 20, 2013 at 10:14 PM 0
Share

Well, actually, I'd like to have a script, that takes a GameObject collects all the data needed automatically. So, I can't just make a public variable.

So, there are really no built in methods to look through the whole hierarchy?

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

16 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

Related Questions

Accessing children of instances vs children of original prefab 1 Answer

Saving customized transform in game 1 Answer

Why Transform and not Prefab? 2 Answers

transforming a prefab randomly at runtime 3 Answers

how to reference a js script attached to a prefabs child from a c# script attached to another prefab? 0 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