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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DocMoriane · Mar 07, 2014 at 05:44 AM · c#hierarchysearchgetcomponentsinchildren

How do I cleanly check if there is a specific component in a parent x positions above the current component in the hierarchy?

Currently I am avoiding 3 children below the parent via gameObject.GetComponentsInChildren but this is causing issues if I have a second child from the highest parent object or the first few children as it appears to go off Depth-First search.

Where the hierarchy looks something like

Parent

|- Child-A

--|- Child-A1

----|- Child-A2

------|- Child-A3

|- Child B

 void Start()
     {
         wiresInSys = new List<ElectricalWire>(gameObject.GetComponentsInChildren<ElectricalWire>());
         systemOn = true;
     }
 
     void interact()
     {
         if(systemOn)
         {
             systemOn = false;
             if(wiresInSys.Count > 4)
             {
                 for(int i=3; i<wiresInSys.Count; i++)
                 {
                     Debug.LogWarning(wiresInSys[i] + " set electricity off");
                     wiresInSys[i].electricityMode = false;
                 }
             }
         }
 }

How could I go about doing this in a manner that captured the children A, A1, A2, and B, as well as any children B might have that are within 3 levels from Parent? Just a note that the children are all prefabs so they have the same name.

Sorry for formatting (editting enabled if anyone wants to edit it better :) ) and if noobish question.

Comment
Add comment · Show 2
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 robertbu · Mar 07, 2014 at 05:59 AM 1
Share

Sounds like you've created an awfully complex situation for yourself. If it were me, I'd figure out some way to tag the game objects I wanted to find as I was instantiating them. You can change the name, the tag, or even add scripts at runtime while you are Instantiating() the objects. If the ones you care about all have the same name or tag, you can walk the Transform tree recursively and grab those you want. If you put a script on all the ones you care about, you can use GetComponentInChildren() using the script you added to find the objects. From a reference to a script, you can get to the game object or transform of that object.

avatar image Jamora · Mar 07, 2014 at 02:50 PM 0
Share

Tags are a possibility, but to me the situation looks like the children in the 3rd level have some functionality that the other GOs don't have. I would try to think for a way to add appropriate scripts to the GOs at instantiation, so GetcomponentsInChildren can be used with just the specific script.

Then there's always the possibility of doing a custom recursive search.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Finding objects with TextMeshPro component. 1 Answer

Step through debug in Visual Studio not updating Unity hierarchy 0 Answers

Where and how are gameobjects stored in files? 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