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
2
Question by cCoding · Jan 29, 2013 at 05:59 PM · objectmodelchild object

Get all child objects within an object?

In blender I "fractured" a house into multiple parts. In unity, how can I properly get all the objects within the house object? My intent is to track when the player collides with each piece.

alt text

Comment
Add comment · Show 3
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 Qvintusdk · Jan 29, 2013 at 07:42 PM 0
Share

If you want to check when the player collides with your objects and want to check if it's a child of "twofloorhome" then you could try. http://docs.unity3d.com/Documentation/ScriptReference/Transform.IsChildOf.html.

Example would be: Assu$$anonymous$$g your using a CharacterController

 using UnityEngine;
 using System.Collections;
 
 public class example : $$anonymous$$onoBehaviour
 {
     void OnControllerColliderHit(ControllerColliderHit hit) {
         if (hit.transform.IsChildOf(GameObject.Find("twofloorhome").transform))
         {
                 //Do somthing
         }
     }
 }
avatar image cCoding · Jan 29, 2013 at 09:01 PM 0
Share

if I want to destroy the children of the home, do I use Destroy(hit.gameObject);?

avatar image cCoding · Jan 29, 2013 at 09:20 PM 0
Share
     void OnCollisionEnter ()
     {
         if (transform.IsChildOf(GameObject.Find("twofloorhome").transform))
         {
             DestroyObject(gameObject);
         }
     }

This works fine when I add the script to each individual fracture. I would like to only add the script to the parent. The other suggestions will not work for either parent or children.

2 Replies

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

Answer by paulaceccon · Jan 29, 2013 at 07:43 PM

I guess you may use GetComponentsInChildren. So, you can do this:

     Transform[] allChildren = GetComponentsInChildren();
     foreach (Transform child in allChildren) {
     // do what you want with the transform
     }


Comment
Add comment · Show 5 · 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 cCoding · Jan 29, 2013 at 08:12 PM 0
Share

This was one of the things I first tried. Unity kept showing this error UnityEngine.Component.GetComponentsInChildren()' cannot be inferred from the usage. Try specifying the type arguments explicitly

avatar image paulaceccon · Jan 29, 2013 at 08:25 PM 1
Share

Have you tried Transform[] allChildren = GetComponentsInChildren(); ?

avatar image MountDoomTeam · Jan 29, 2013 at 09:04 PM 0
Share

ins$$anonymous$$d of finding the child GameObjects you always just find their transform, the transform is just part of them that says their position. it's what everyone uses in unity to find children

avatar image Pyroglyph · Aug 12, 2014 at 07:04 PM 0
Share

I get an error when I use this, it expects a ')' but gets 'child'. I understand what it means but no idea why it's throwing that error.

avatar image GameDevSA · Aug 02, 2017 at 02:11 AM 0
Share

I think in C# it should be:

 Transform[] allChildren = GetComponentsInChildren<Transform>();

I just tried using it to deactivate all of the children of an object, and it worked perfectly in C#. The precise code was:

 Transform[] allChildren = GetComponentsInChildren<Transform>();
         foreach (Transform child in allChildren)
         {
             child.gameObject.SetActive(false);
         }
avatar image
8

Answer by Paulo-Henrique025 · Jan 29, 2013 at 09:38 PM

 foreach(Transform child in gameObject.transform)
 {
         //Do your stuff
 }
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 Pyroglyph · Aug 12, 2014 at 07:04 PM 0
Share

I get an error when I use this, it expects a ')' but gets 'child'. I understand what it means but no idea why it's throwing that error.

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

14 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

Related Questions

Can't Add Script 1 Answer

Accessing a Certain Script 1 Answer

Fetching Objects that belongs to other script, with name or tag 1 Answer

Check for collisions with script outside of object with collider 0 Answers

Calling an object from a different JS Script and Instantiate it 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