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 StaggerLee · Mar 18, 2014 at 03:12 AM · parentchildnoobforeach

Problem with finding compnent of immidate children

Hi all im having problems finding the components of immediate children.

Im trying to make a "power" system that starts at one parent object at say power 10, then as it goes through its children (all with the same script) they divide up the power.

transform.GetComponentsInChildren() will not work because firstly it calls its own LocalPower script even though it is not a child, and secondly it calls all of the sub children. I learnt this enough from google.

so next i tryed to use foreach (Transform child in transform) { nNode = child.transform.GetComponent (); }

However not all of its child objects have the script, as only power 'nodes' and things that use power use the script.

I tried to make a list and only add nNode if it found the script but could not find a way to do that- and im feeling pretty dicy about the whole thing.

First post, im a total noob im afraid, thank you!

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 robertbu · Mar 18, 2014 at 03:20 AM

You can make either one work. Start with the second one first. Just check to see if you get a null back from GetComponent():

    foreach (Transform child in transform) {
         if (child != transform) {
              SomeScript nNode = child.GetComponent<SomeScript>();
              if (nNode != null) {
                  // Do whatever with nNode
              }
         }
     }

As for the other one, just check the transform.parent:

 SomeScript nNodes[] = gameObject.GetComponentsInChildren<SomeScript>();
 
 foreach (SomeScript nNode in nNodes) {
     if (nNode.transform.parent == transform) {
          // Do whatever with nNode
      }
 }
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 StaggerLee · Mar 18, 2014 at 05:13 AM 0
Share

Worked fantastic! I need to get my head into programmer mode, i had not thought to check if the child was the parent.

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

20 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

Related Questions

Make a simple tree 1 Answer

Unparenting a transform in a foreach loop in the parent transform 3 Answers

using foreach transform child, unity gets stuck 2 Answers

Get Children of Child and Deactivate/Activate on Demand 1 Answer

what happens when Unit in Hierarchy is destroyed? 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