- Home /
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?
Where did you get this magical InChildren
function from? It's not in the documentation.
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
@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)
@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.
Haraback, GetComponentInChildren doesn't take that argument, GetComponentsInChildren does, but yes, you are right. I forgot about that version of the function.
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;
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).
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
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!
Your answer
Follow this Question
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