- Home /
Affect child's child from main parent's script?
Hello, I need to be able to manipulate a child object of a parent. The problem is, using code like this
var orb = this.transform.Find("healthorb");
//Do stuff to the object called healthorb even though this script is attached to it's parent
Will not work, as the child I need to manipulate is not a direct child of the main parent, it is parented to another child of the main parent.
Is there any easy way to do this while keeping all the scripting on the main parent?
What I am trying to do more specifically is be able to turn the renderer.enabled function of a muzzle flash object on/off through a script on the main character. Problem is, the muzzle flash object is parented to the hand, which is a ways down the chain from the main character.
Any ideas?
Answer by Montraydavis · Oct 28, 2012 at 10:36 PM
gameObject.transform.GetComponentInChildren ( "blaaa" ) would be the most efficient way.
var script : ScriptName;
script = GetComponentInChildren(ScriptName);
script.DoSomething ();
Unity SDK : http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentInChildren.html
You could use that as many times as you need to get whatever child .
This was it! Here was what worked for me:
var allChildren = gameObject.GetComponentsInChildren(Transform); for (var child : Transform in allChildren) { // do whatever with child transform here if (child.name == "muzzleflash") { child.renderer.enabled = false;//or true, or flashing, or whatever
}
}
Answer by NikEy · Oct 29, 2012 at 12:19 AM
I think that answer might be helpful to you: http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html
e.g. something along the line of: parent.transform[0].Find("healthOrb")
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
how do I get reference to a parent or a child? 2 Answers
Find children by tag from Player 1 Answer
find ALL children of a parent 1 Answer
Finding child problems.. 1 Answer