How to find child object, without name or tag.

Hello,

Before I explain my issue, I’d like to point out that this isn’t a “ohmigosh I’m stuck I cannot continue without help ever” I can very easily continue; there is an easy-ish workaround, but it ain’t pretty.

I’m working on a group project, and I’m creating a chandelier that’s supposed to be able to have its rope burnt whenever our main character attacks it. I’m using parent + child to “know” which part of the rope is above, and which part is below the targeted rope.

Hierarchy

The top-most is an immovable, empty game object for easy implementation.

the script I’m working with right now is as follows:

private GameObject RopeAbove;
public GameObject RopeBelow;
private bool IsBurning;

// Use this for initialization
void Start () {
	RopeAbove = transform.parent.gameObject;
	Debug.Log(RopeAbove);
	Debug.Log(RopeBelow);
}

Of course, this works, but it’s not a “pretty” solution. I’d like to know if you can call a child in the same way I have called the parent.

Thanks in advance!

Looks like each individual transform has only one child, so transform.GetChild(0) ought to work:

if (transform.childCount > 0)  // Prevents ArgumentOutOfRangeException
    RopeBelow = transform.GetChild(0);

If you reference the RopeBelow object from script, you’ll need to make sure it has a value before accessing its members:

if (RopeBelow) // Prevents NullReferenceException
    // Do stuff with RopeBelow

@Vicarian helped me out; however, there was a problem with using “transform.GetChild(0)”, instead you should use “transform.GetChild(0).GameObject”. This will make it return the gameobject instead of the transform, which is what I wanted.

Fixed code:

private GameObject RopeAbove;
private GameObject RopeBelow;
private bool IsBurning = false;

// Use this for initialization
void Start () {
	RopeAbove = transform.parent.gameObject;
	Debug.Log(RopeAbove);
	if (transform.childCount > 0){  // Prevents ArgumentOutOfRangeException
		RopeBelow = transform.GetChild(0).gameObject;
	} 
	Debug.Log(RopeBelow);
}

// Update is called once per frame
void Update () {
	if(IsBurning == true){
		//RopeAbove.BurningScript.IsBurning = true;
		//RopeBelow.BurningScript.IsBurning = true;
	}
}