Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
10
Question by sadowlight123 · May 20, 2017 at 12:09 PM · objectparentchildren

Find children in parent

Hello all , So this is my question . I have a game object that contains 4 children . I want it to get one of those children (can search it by name) ? how do I do this through code? Thanks a lot in advance.

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

8 Replies

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

Answer by UnityLib43 · May 20, 2017 at 01:27 PM

Try this: gameobject.transform.Find("ChildName") ; on the parent gameobject

Comment
Add comment · Show 3 · 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 sadowlight123 · May 26, 2017 at 09:35 AM 4
Share

Does it only loop in the paren'ts children or in all of the gameobjects in the scene ?

avatar image UnityLib43 · May 31, 2017 at 06:59 PM 6
Share

It loop in the parent's children only .

avatar image GoodMath · Apr 03 at 08:45 AM 0
Share

gameObject*

avatar image
10

Answer by CapeGuyBen · May 20, 2017 at 01:07 PM

In Unity, the hierarchy information is stored in the Transform component rather than the GameObject itself. You can find a child with a given name using the Find method on the Transform:

 GameObject GetChildWithName(GameObject obj, string name) {
     Transform trans = obj.transform;
     Transform childTrans = trans. Find(name);
     if (childTrans != null) {
         return childTrans.gameObject;
     } else {
         return null;
     }
 }


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 sadowlight123 · May 26, 2017 at 09:46 AM 0
Share

Thank you for your answer . Does your solution only loop in the paren'ts children or in all of the gameobjects in the scene ?

avatar image AldeRoberge · Jul 07, 2021 at 12:55 AM 1
Share

Even better :

 public static GameObject GetChildWithName<T>(this T obj, string name) where T : Component
         {
             Transform trans      = obj.transform;
             Transform childTrans = trans.Find(name);
             
             if (childTrans != null)
             {
                 return childTrans.gameObject;
             }
 
             return null;
         }


Edit : If you need deep (more than 1 children down) : https://answers.unity.com/questions/799429/transformfindstring-no-longer-finds-grandchild.html

avatar image
6

Answer by Artaani · May 20, 2017 at 01:08 PM

Place this code on parent object:

 foreach (Transform eachChild in transform) {
     if (eachChild.name == "NameWhatYouNeed") {
         Debug.Log ("Child found. Mame: " + eachChild.name);
     }
 }

Update: Method above is better. Didn't know that.

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
3

Answer by nitinDhami · May 20, 2017 at 01:27 PM

you can use tranform.getchild(index) to get any one of them

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 juzimmer · Jan 07 at 02:51 PM 0
Share

You would actually use

transform.GetChild(yourIndexHere);

avatar image
2

Answer by metixgosu · May 10, 2021 at 06:59 AM

It is Transform not transform. https://docs.unity3d.com/ScriptReference/Transform.html

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
  • 1
  • 2
  • ›

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

82 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 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 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

Is there a way to limit a parent object's child count to only 1 without individually doing it with each transform? 0 Answers

Create a series of buttons out of the names of a object childs names. 0 Answers

Create multiple instances of an object 2 Answers

Need help, object parent object 1 Answer

Children don't move with parent 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