- Home /
Best way to Reference Child from Parent's script?
Hi Guys,
Been all over the net and got a lot of info on how to do this - but nothing saying which method is best for performance - nor why to use a particular method over another.
Also, I am confused as to why a child object cannot be directly referenced in its parents script?
If I have a parent object Man with child HandL -
Why is it that in the Man class,
this.HandL.transform.localPosition = (new Vector3(0,1.3f,0));
Does not work??! - It says, HandL is not in current context??
Is this because HandL was added to the parent, man, in the interface? Is there a way to declare that HandL is a child of man - and then reference it directly using
this.HandL.transf......
//-------------------------------------
Otherwise I have found three ways to do this - which is best for performance? esp on smartphones?
//--------------------------- var hand = GameObject.Find("HandL"); hand.transform.localPosition = (new Vector3(0,1.3f,0)); //---------------------------- //--------------------------- var hand = transform.Find("HandL"); hand.transform.localPosition = (new Vector3(0,1.3f,0)); //--------------------------
//-------------------------- other.GetComponent().waving(); //-------------------------- //-- in this last one the moving is done in the hand class
As a noob I find this simple stuff really confusing. Please let me know which way is best and also if there is a simple way to get direct access to my children from the parent class.
Thanx,
GreenLotus.
Answer by Eric5h5 · Nov 06, 2012 at 07:23 AM
The child can't be directly referenced like that because the child doesn't exist in your code, it exists in the Unity project. Therefore you need to get a reference to it. As for performance, it's completely irrelevant, since you're only doing it once (or you should be). You should probably use transform.Find in this case, because GameObject.Find will just get the first object it finds with that name, which isn't necessarily the child you want. Using transform.Find makes it clear that you're getting a child, not just some other game object in the scene.
Answer by Sonaten · Nov 06, 2012 at 12:25 PM
I access objects and children in my scenes like this (I know I'm not addressing if it is the best method or not, but I believe it to work quite well).
private GameObject sceneParent, myRocket;
void Start () {
sceneParent = GameObject.Find("SceneParent");
myRocket = GameObject.Find("Raket");
myRocketDeepChild = myRocket.transform.GetChild(0).GetChild(0).GetChild(1).gameObject;
//the Deep child is one of the indermost children in the structure.
//It is simply a container for graphics that I want to handle.
}
This way, I keep the important children in variables (pointers really), and in any other case I would just use transform.GetChild(index).
Take note that GameObject.Find searches all objects in the scene, so I'd suggest not using it in run time; and, prefereable before any prefabs are instantiated.
I hope you can use this.
Answer by Xathereal · Nov 06, 2012 at 01:01 PM
Finding things by name is generally a bad thing to do, but sometimes (like in this situation) it is a viable solution.
Try this code which is based upon the fact that every gameObject has a transform (correct me if I am wrong please):
Transform[] transforms = this.GetComponentsInChildren<Transform>();
foreach(Transform t in transforms)
{
if (t.gameObject.name == "Child")
{
Debug.Log ("Found " + t);
}
}
Hope this helps! =D
Does this have any advantages over the "Find" function of transform? Is there any other difference?
I guess the real difference is that it would be better if you had many child objects with the same name you would be able to reference all of them. e.g. a herd of monsters that attack in a group but only if you enter their territory. I don't think it's any different in this case though.