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 felixfors · Feb 07, 2014 at 11:43 AM · c#childchildrenenableactivate

C# Find component InChildren

Hey there! I have gone through some unity tutorials and Im currently trying out to make a third person shooter game, connected to photon. I have the main network script inside a gameobject that spawns in my player at 0,0,0 position and everything works great, the only problem is that I need to activate the moving script/mouse look etc after the character have been spawned into the world and this works fine just that I need to turn on my mouse look Z script " for moving the torso" this would have worked if it was a component direct connected to my player but it needs to be connected to the torso bone so it is a children for the player. I want to activate the players child " Playercontroller->Armature.001->Bone->Bone.001 The script is laying on the " Bone.001" but I cant get it to work this is one example that I have tried

  myPlayerGO.transform.InChildren("Bone.001").GetComponent("torso").enabled = true;

but it dosnt find the child Bone.001 why?

Comment
Add comment · Show 5
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 Benproductions1 · Feb 07, 2014 at 11:44 AM 0
Share

Where did you get this magical InChildren function from? It's not in the documentation.

avatar image felixfors · Feb 07, 2014 at 11:45 AM 0
Share

do you mean that this question have been asked before or that I asked more then one question in my thread? I tried to google around before I posted this if I had found something that could have helped me I wouldnt have made this question " logic", if it was the second thing about asking for two many questions in the same thread well it wasnt suppose to be like that, I was just wondering why I cant find my players child-child

avatar image Harabeck · Feb 07, 2014 at 10:43 PM 1
Share

@Alik

There is no reason to use poncho's answer ins$$anonymous$$d of GetComponentInChildren(), you just have to pass it the optional paramter to return inactive: GetComponentInChildren(true)

avatar image Benproductions1 · Feb 07, 2014 at 11:15 PM 0
Share

@felixfors In the moderation queue there were two of your questions, both the same. I rejected the other and posted this one. Please do not post the same question more than once and have patience for it to go through the moderation queue.

avatar image AlkisFortuneFish · Feb 08, 2014 at 12:49 AM 0
Share

Haraback, GetComponentInChildren doesn't take that argument, GetComponentsInChildren does, but yes, you are right. I forgot about that version of the function.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ashique · Feb 07, 2014 at 12:03 PM

you can try as

myPlayerGO.transform.FindChild("Bone").transform.FindChild("Bone.001").GetComponent().enabled = true;

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 AlkisFortuneFish · Feb 08, 2014 at 12:35 AM 0
Share

Apart from the fact that this will trigger an exception if any of it is null, to traverse the hierarchy like that you can use path notation, ie. FindChild("Armature.001/Bone/Bone.001"), there is no reason to have two FindChild calls.

It will also not work if the Bone.001 is disabled, as you cannot Find() a disabled object. If the object is not disabled, a simple GetComponentInChildren will do, otherwise you need to manually traverse the hierarchy (see: Poncho's answer).

avatar image
1

Answer by poncho · Feb 07, 2014 at 08:21 PM

a little recursivity and some cycles are more than enough to find anything inside anything

 method(Transform objectTransform)
 {
     if(objectTransform.ChildCount > 0)
     foreach(Transform trans in ObjectTransform)
     {
         method(trans)
     }
     if(objectTransform.GetComponent<MyComponentOrScript>() != null)
     {
         //do what you need with the component
     }
 }

thats the main, idea, do not forget to add a break in your recursivity or cycles, good luck and happy coding

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
avatar image
1
Wiki

Answer by AlkisFortuneFish · Feb 07, 2014 at 08:45 PM

Any particular reason not to use GetComponentInChildren() on the root object? I mean, I presume your object hierarchy itself is all active, only the script itself disabled. If the child itself is disabled, it will not be found, in which case you will have to use Poncho's answer to find it (and if you do, please accept his answer). I do have to ask though, if the child itself is disabled, why?

If you use GetComponentsInChildren(true), you will get the component regardless of whether it's active or not. Thanks to Haraback for reminding me of that version of the method, with that argument!

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

23 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 avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Switch for children not working 1 Answer

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

Cycling Weapons, using C# int string problem 1 Answer

Enable Child Component 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