Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 fjalla · Jan 08, 2013 at 05:18 PM · errorgetcomponentinchildren

Access components in ALL children

Hi. How do I access components in all of the children? When I used GetComponentInChildren it only changed it in one of the children, but I want all 9 to be changed.

Cheers.

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
Best Answer

Answer by Landern · Jan 08, 2013 at 05:33 PM

 This wasn't tested, just an idea of iterating over comps in transform(gameObject.transform).  Anyways you should be able to get the general idea i hope.


 
 //unity script/javascript
 
 for (var child : Transform in transform) {
     for (var comp : Component in child.GetComponentsInChildren(Script)) {
          comp.name = "blah";
     }
 }
 
 // c#
 
 foreach(Transform child in transform)
 {
   foreach(Component comp in child.GetComponentsInChildren<SomeScript>())
   {
     comp.tag = "blah";
   }
 }
Comment
Add comment · Show 10 · 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 fjalla · Jan 08, 2013 at 05:39 PM 0
Share

So I have to use GetcomponentsInChildren?

avatar image Wolfram · Jan 08, 2013 at 06:06 PM 0
Share

Yes .

avatar image fjalla · Jan 09, 2013 at 05:21 PM 0
Share

So for example if I wanted to set a boolean "blah" = true in "blahScript" in all children, how would I do it?

Is it something like this? :

for (var child : Transform in transform) { for (var comp : Component in child.GetComponentsInChildren(Script)) { comp.name = "blahScript"; } }

comp.blah = true;

avatar image Wolfram · Jan 09, 2013 at 05:33 PM 0
Share

Actually, you don't need the outer loop, and you'll need to put the assigment into the loop, of course:

 for (var comp : myScriptName in gameObject.GetComponentsInChildren(myScriptName)) {
     comp.blah = true;
 }

Note if the object where you attach this script to also has the Component "myScriptName" attached, it will also be processed, although it's not a child. To prevent this, you'd need the outer for-loop @Jordan suggested.

avatar image PersonMon Wolfram · Nov 17, 2020 at 05:46 PM 0
Share

Is there a version of this in c#?

avatar image Hellium PersonMon · Nov 17, 2020 at 06:16 PM 1
Share
 foreach (ScriptName component in gameObject.GetComponentsInChildren<ScriptName>())
 {
      component.blah = true;
  }
Show more comments
Show more comments
avatar image fjalla · Jan 09, 2013 at 05:57 PM 0
Share

Thanks :) This saved me loads of time. I thought if I couldn't get this to work I'll would have to do it through variables :D

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Getting index of the smallest number in a list 1 Answer

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System. 0 Answers

Zombie Round Script Help 1 Answer

Android Building problem 1 Answer

Problems with creating custom EditorGUI for script 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