• 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
0
Question by adamchakov · Mar 03, 2014 at 12:56 PM · parentchildchildrenfind

find ALL children of a parent

hey guys , i want to find ALL the CHILDREN of this gameobject , but this script only find me one child , help please , thanks.

 var fruit : GameObject;
 
 function Start() {
 fruit = GameObject.Find("fruit");
 fruit.collider.enabled = false;
 fruit.renderer.enabled = false;
 fruit.animation.Play("wumpa");
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Mar 03, 2014 at 01:07 PM

Your code doesn't find any children. It finds a game object named 'fruit' somewhere in the scene. If you only have a single level of children, you can use the code at the reference page for Transform to walk these children:

 for (var child : Transform in transform) {
     child.collider.enabled = false;
     child.renderer.enabled = false;
     child.animation.Play("wupa");
 }

If you have multiple levels of children and need to find them all, you can use GetComponentsInChildren():

 var children = gameObject.GetComponentsInChildren(Transform);
 for (var child : Transform in children) {
     child.collider.enabled = false;
     child.renderer.enabled = false;
     child.animation.Play("wupa");  
 }

Note that GetComponentsInChildren() also returns the parent, so you may need to add an explicit check.

Comment
Add comment · Show 12 · 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 adamchakov · Mar 03, 2014 at 02:05 PM 0
Share

thanks a lot for the reply @robertbu , i want to find these children with the tag "fruit" , i used FindGameObjectsWithTag , but it didn't work ! help please :s , thanks

avatar image Jamora · Mar 03, 2014 at 02:59 PM 0
Share

You can check for the tag with if(child.tag == "fruit")

avatar image adamchakov · Mar 03, 2014 at 04:03 PM 0
Share

how to add to the script please @Jamora help

avatar image robertbu · Mar 03, 2014 at 05:30 PM 0
Share

You can do this for first level children:

 for (var child : Transform in transform) {
     if (child.tag == "fruit") {
         child.collider.enabled = false;
         child.renderer.enabled = false;
         child.animation.Play("wupa");
     }
 }

You can alter the other example in the same way if you need all children. Note that 'FindGameObjectWithTag' finds all game object in the scene with a specific tag, not just for a particular object. But if you are only talking about one level, you could look through the list returned and only process those who's transform.parent is the game object you want as a parent.

avatar image adamchakov · Mar 03, 2014 at 05:39 PM 0
Share

hey @rebertbu thanks a lot it's working , but since my fruit is a child of another "child" and this "child" is a child of the Parent which has this script attached to it, so how can i get this to work ? . PS: my fruit is a child of another child , because i had a problem with the animation of the fruit , the animation doesn't play local , so that's why i put the fruit in an emptyobject.

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.

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

24 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

Related Questions

(C#) How do I run something in my child's script? 2 Answers

Make a simple tree 1 Answer

How To Get List of Child Game Objects 14 Answers

accessing all of a colliders parents children 1 Answer

Finding child problems.. 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