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
0
Question by Golan2781 · Feb 24, 2013 at 01:41 PM · hierarchygetcomponentinchildren

Detecting hierarchy setup when dynamically setting referenes

We have some rather complex object setups for our game objects/prefabs (at least given their amount) so most of our scripts are set to automatically resolve references if not manually set, thus allowing quickly setting up units just by dropping in modules without having to resolve obvious references.
For example, the controller module will look for weapon, engine, AI and HP scripts in the object's hierarchy unless specifically given them.

     if ( weapons.Length == 0 )
         weapons = transform.GetComponentsInChildren.<Weapon_Base>();
     if ( engine == null )
         engine = transform.GetComponentInChildren.<Engine_Thruster>();
     if ( operator == null )
         operator = transform.GetComponentInChildren.<Operator_Base>();
     if ( bodyHub == null )
         bodyHub = transform.GetComponentInChildren.<Body_Hub>();

The Hierarchy is rather flat, like this:

 > root
 | o Controller Script
 | o Body Script
 | o Engine Script
 |-> Colliders
 | |-> BoxCollider
 | \-> ...
 |-> WeaponA Container
 |   o WeaponA Script
 \-> WeaponB Container
     o WeaponB Script

Now we'd like to extent the complexity of our objects with sub-components - think of independent turrets, destroyable engines, the like. This means that a component would scan the hierarchy for possible script references, but only until it hits another component of its kind - for example, the root controller would still store all weapons but those in a sub-hierarchy of say a turret controller.

 > root
 | o Root Controller Script
 | o Root Body Script    # belongs to root controller
 |-> WeaponA Container
 |   o WeaponA Script    # belongs to root controller
 |-> Turret Root         # root controller won't enter here
 | | o Turret Controller Script
 | | o Turret Body Script    # belongs to turret controller
 | \-> WeaponB Container
 |     o WeaponB Script      # belongs to turret controller
 \-> WeaponC Container
     o WeaponC Script    # belongs to root controller

So what we'd need is some way to walk through the hierarchy, just as one would walk through directories in a filesystem, or a way to discern whether between the searching component and the found one there is a specific component in between. Or is there even an inbuilt function for this, to limit the depth of searching components?

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
0

Answer by nsxdavid · Feb 25, 2013 at 01:33 AM

I can't tell from your description exactly what you are after, but you can scan the hierarchy yourself very easily.

First, the transform is an iterator, so:

 foreach(Transform child in transform) {
   // do something with child
 }

If you want, you can easily stuff this in a method so it does this recursively:

 void ScanHiearchy(Transform t)
 {
   foreach(Transform child in t) {
     // do something with child
     ScanHiearchy(child); // Recurse!
 
   }
 }

But from the looks of what I think you want to do, you don't want to recurse. So each controller can just traverse the first level of children (the initial code sample above), and on each child do the appropriate GetComponent() call. If not null, store the component in the root controller's collection of such things. Each root controller just does that.

So something like:

 // Example for Root Controller Script
 // Assumes someplace Weapons is defined as a listof RootBodyScript components.
 foreach(Transform child in transform) {
   RootBodyScript rbs = child.GetComponent<RootBodyScript>();
   if(rbs)
     weapons.Add(rbs);
 }

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

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

10 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

Related Questions

Accessing text in GUIText Component: Slider.GetComponentsInChildren().text is invalid 2 Answers

Hierarchy question 1 Answer

Text Labels For Prefabs in Hierarchy Turn Pink 2 Answers

Scene corrupted - enpty hierarchy 0 Answers

Script only works in the order of the hierarchy 2 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