GetComponentsInChildren does not work as detailed?

Hi all,

Having some issues with GetComponentsInChildren

On version 4.6.5f1; which I need to stay on for a while (don’t ask! xD)

According to the docs, which using the override I should be able to get all components on the given GameObject and all of its children, regardless of any of the objects being active. Or perhaps I am reading that slightly wrong?

Couple of questions:

  • Should this care if the component is enabled / disabled (which is frustrating)
  • Does anyone actually find this works with inactive objects (see example code)

Bounds lBounds = new Bounds();

GameObject lGo = new GameObject(“PARENT”);
lGo.AddComponent();
lGo.transform.localScale = new Vector3(50, 50, 50);
lGo.SetActive(false);

GameObject lChildGo = new GameObject(“CHILD”);
lChildGo.AddComponent();
lChildGo.transform.parent = lGo.transform;
lChildGo.transform.localScale = new Vector3(20, 20, 20);

Collider[ ] lColliders = lGo.GetComponentsInChildren(true);

//encapsulate all collider bounds
if (lColliders != null &&
lColliders.Length > 0)
{
lEnumerator = lColliders.GetEnumerator();
while (lEnumerator.MoveNext() == true)
{
lBounds.Encapsulate(((Collider)lEnumerator.Current).bounds);
}
}

Debug.Log(lBounds.size);

This code returns 0 size always
I would expect bounds to be 1000, which works when the GameObject is active
If the child is inactive and the parent is active, the size is 50
So inactive objects are always ignored?
Same result if component is disabled as inactive

It should find disabled components, but it won’t find anything on inactive children.

If you need to find components on inactive children, recursively iterate on the parent’s transform, which returns all children active or inactive.

From what I am testing, it does not find disabled components either

The doc says:
“Should Components on inactive GameObjects be included in the found set?”
Which surely implies any inactive objects?

Neither case seems to work, tried changing the setup of the objects to:

GameObject lGo = new GameObject(“PARENT”);
BoxCollider lCollider = lGo.AddComponent();
lGo.transform.localScale = new Vector3(50, 50, 50);
lCollider.enabled = false;
//lGo.SetActive(false);

GameObject lChildGo = new GameObject(“CHILD”);
BoxCollider lChildCollider = lChildGo.AddComponent();
lChildGo.transform.parent = lGo.transform;
lChildGo.transform.localScale = new Vector3(20, 20, 20);
lChildCollider.enabled = false;

With the same failed result.
From all test cases this only works on active objects / components

The problem is somewhere in your bounds segment. Also, use code tags.

using System.Linq;
using UnityEngine;

public class Slot : MonoBehaviour
{
    private void Start()
    {
        GameObject lGo = new GameObject("PARENT");
        lGo.AddComponent<BoxCollider>();
        lGo.transform.localScale = new Vector3(50, 50, 50);
        lGo.SetActive(false);

        GameObject lChildGo = new GameObject("CHILD");
        lChildGo.AddComponent<BoxCollider>();
        lChildGo.transform.parent = lGo.transform;
        lChildGo.transform.localScale = new Vector3(20, 20, 20);

        Collider[] lColliders = lGo.GetComponentsInChildren<Collider>(true);

        Debug.Log(lColliders.Count());
    }
}

Works fine.

1 Like

Thanks guys, your right the GetComponentsInChildren is working fine!
Bounds is return as always 0 size when object / component is inactive :slight_smile:
Although still frustrating :frowning: