- Home /
The question is answered, right answer was accepted
Find Child Objects by Name
I have a game object, and it has two children objects inside it. When the player collides with a trigger on the parent object I want the two children objects to move in a certain way. The script is attached to the parent. I was thinking it could find the children by name. Lets call them Child_1 and Child_2 and the parent object will be called ParentObj
Im just not sure how to go about finding the children.
I have laid out the basics. I just need a way to find the actual child and plug it into my little place holders.
using UnityEngine;
using System.Collections;
public class StandardDoor : MonoBehaviour {
private Vector3 pos;
public Vector3 target;
void Start() {
pos1 = Child_2 GOES HERE .transform.position;
pos1.y += 0;
target1 = Child_2 GOES HERE .transform.position;
target1.y += 3;
pos2 = Child_2 GOES HERE .transform.position;
pos2.y += 0;
target2 = Child_2 GOES HERE .transform.position;
target2.y -= 3;
}
void OnTriggerEnter (Collider other) {
if (other.tag == "Player") {
Debug.Log("collide");
Child_1 GOES HERE .transform.position = Vector3.MoveTowards(target, pos, 0.1f);
Child_2 GOES HERE .transform.position = Vector3.MoveTowards(target, pos, 0.1f);
}
}
void OnTriggerExit (Collider other) {
if (other.tag == "Player") {
Debug.Log("not collide");
Child_1 GOES HERE .transform.position = Vector3.MoveTowards(pos, target, 0.1f)
Child_2 GOES HERE .transform.position = Vector3.MoveTowards(pos, target, 0.1f)
}
}
}
thanks in advance.
You could use transform.FindChild(), and get a GameObject from that.
GameObject go = this.transform.FindChild("$$anonymous$$illZone").gameObject;
go.SetActive(true);
Answer by fafase · Apr 18, 2014 at 01:42 PM
Children are stored on the transform of the parent object. So iterating through the transform you can check for the name:
foreach (Transform t in transform)
{
if(t.name == "Child1")// Do something to child one
else if (t.name == "Child2")// Do something to child two
}
other way if you have a lot of children to find then you can create a dictionary of children:
Dictionary<string, Transform> dict = new Dictionary<string, Transform>();
void Start(){
foreach(Transform t in transform)
{
dict.Add(t.name, t);
}
}
then you can fetch it by name:
void OnCollisionEnter(Collision col){
Transform child = dict[col.gameObject.name];
}
Actually this one is faster than the other way since it will find the matching one at O(1) while the other one is O(n) where n is the amount of children.
Answer by HarshadK · Apr 18, 2014 at 01:39 PM
Transform.Find() is what you want. Refer: Transform.Find
im getting this error though :( Assets/Doors/StandardDoor.cs(13,17): error CS0103: The name `doorTop' does not exist in the current context
void OnTriggerEnter (Collider other) {
if (other.tag == "Player") {
Debug.Log("collide");
doorTop = transform.Find("StandardDoor/StandardDoor_TOP");
//doorBottom = transform.Find("StandardDoor/StandardDoor_BOTTO$$anonymous$$");
doorTop.Rotate(Time.deltaTime*20, 0, 0);
}
}
Transform. Or make it a GameObject and call transform.Find().gameObject.
so
GameObject doorTop = transform.Find("StandardDoor/StandardDoor_TOP").gameObject;
is that correct?