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 kmccmk9 · Dec 26, 2011 at 08:44 AM · c#parentcomponentchildren

Script to add script to all children

Hello, I'm trying to use a script to add a script component to all of the children of the object. This script is not directly placed on the object so I'm not sure if I need to use GameObject.Find() in some cases. Below I've posted some code I've put together that doesn't work. It just keeps giving errors about the add component function. Any help would be appreciated

 void Start () {
     Transform[] allChildren = GetComponentsInChildren<malenude>();
     femaleNude = GameObject.Find("femalenude");
     femalenudeScript = femaleNude.GetComponent<FemaleNude>();
 femalenudeScript.ChangeEnabled(false);
     foreach (Transform child in allChildren) {
     child.AddComponent(MaleNude);
     maleNude = GameObject.Find(Transform);
     malenudeScript = maleNude.GetComponent<MaleNude>();
 malenudeScript.ChangeEnabled(false);
     }
 }
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
8
Best Answer

Answer by aldonaletto · Dec 26, 2011 at 09:54 AM

Any compiled script in your project is a valid component, and may be added with AddComponent like any other native component (Rigidbody, AudioSource etc.).
Supposing you want to add the script FemaleNude.cs to all children, you should do something like this (parent script):

void Start(){
  foreach (Transform child in transform){
    child.AddComponent<FemaleNude>();
  }
}
NOTE: C# and JS can't see each other at compile time, thus if FemaleNude was a JS script, this would produce an error (kind of "FemaleNude unknown in the current context" or whatever).
If you need to add JS scripts, they must be compiled in a previous stage - see Script Compilation.
Comment
Add comment · Show 13 · 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 kmccmk9 · Dec 26, 2011 at 03:58 PM 0
Share

Thank you for your reply. Ok so I changed the code to what you have above. But I'm still getting an error that it can't be used as a statement

avatar image aldonaletto · Dec 26, 2011 at 07:37 PM 0
Share

What's exactly the error message?

avatar image kmccmk9 · Dec 26, 2011 at 07:48 PM 0
Share

The error message reads, "Only assignment, call increment, decrement, and new object expressions can be used as a statement"

avatar image kmccmk9 · Dec 27, 2011 at 02:23 AM 0
Share

Now its saying Transform does not contain a definition for AddComponent

avatar image aldonaletto · Dec 27, 2011 at 02:36 AM 0
Share

$$anonymous$$y fault, it should be:

   child.gameObject.AddComponent<FemaleNude>();
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move the parent object to a child of one of its children? 2 Answers

How to move the parent object to a child of one of its children? 1 Answer

Rotate similar child objects around own origin 2 Answers

onTriggerEnter is unclear to me... 3 Answers

Deactivate Parent AND Children (C#) 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