- Home /
Check if the child of an object is active not working!!!
I have a container GameObject that has multiple children with in turn children. I need to take only the collisions of the children that are active, but when I check if achild is active, either with activeSelf or with activeInHierarchy, it always returns true, no matter if the child is active or not. Has no sense. I don't know what I'm doing wrong or if is a bug in Unity 2021.2.28. Does anyone have any ideas? Thanks.
Have you tried recreating the problem in isolation? Start with a blank scene and add a single, empty gameObject and see if you can write a test script to successfully get its active status. If that works, you can gradually re-add the elements of your earlier scene one by one, and see where the problem re-emerges.
if is a bug in Unity 2021.2.28
Chances are you did something wrong...
But without any code, no description of the hierarchy, etc, it's impossible to help you
It's either a bug or:
- You clicked on the eye instead of ticking the box, which is in the inspector not the hierarchy. 
- You made a mistake in the If-function. 
Then again, it is impossible to help without seeing the code or hieracrhy as hellium commented.
Answer by Clegert · Mar 08 at 03:30 PM
I found where the problem was! I was making a mistake. I'm sorry if you guys wasted your time. The only thing I can do in my defense, in case someone comes across this confusion, is to explain the situation. When adding multiple children as boxes, GOBox(A).ChildBox(B).ChildBox(C).ChildElement(D) and the last box has a child element (D). What I did is that OnStart deactivated the childBox(B), therefore C and D were deactivated automatically, but when opening the GOBox(A) and activating the childBox(B) all the contained elements (C and D) were also activated . I had to do an OnStart procedure to SetActive(false) the items contained in the GOBox recursively from D to B. Best regards.
Answer by Alexander_Atanasov · Mar 07 at 07:44 PM
Maybe something like this:
 using UnityEngine;
 using System.Collections.Generic;
 public class TestScript : MonoBehaviour {
     public GameObject objectToGetChildrenFrom;
     public List<GameObject> activeChildren;
     private void Update() {
         activeChildren.Clear();
         for(int i = 0; i < objectToGetChildrenFrom.transform.childCount; i++) {
             if (objectToGetChildrenFrom.transform.GetChild(i).gameObject.activeSelf) {
                 activeChildren.Add(objectToGetChildrenFrom.transform.GetChild(i).gameObject);
             }
         }
     }
 }
I just loop through all children of this object and if its active I add it to the list with active children. I can't understand why it won't work if you give more information it would be better.
Thank you for your answers!
@Cakebox I did what you suggested manually and it worked fine. The problem is that the children are created by script from a Prefab and I inactivate them with SetActive(false), the child gameObjects are inactive and hidden but when I want to check if they are active by script they are always active. The code is something like this.
 static public bool GetCollidersBounds(GameObject go, bool searchMeshInChilds, ref Bounds bounds) {
         Component[] comps;
         List<Collider> collLst;
         Collider collTmp;
         int i;
         collLst = new List<Collider>(0);
         collTmp = go.GetComponent(typeof(Collider)) as Collider;
         if(collTmp) {
             collLst.Add(collTmp);
         }
         if(searchMeshInChilds) {
             comps = go.GetComponentsInChildren(typeof(Collider));
             if(comps != null && comps.Length > 0) {
                 Debug.Log("    >> GetCollidersBounds Childs");
                 for(i = 0; i < comps.Length; i++) {
                     collTmp = comps[i] as Collider;
                     if(comps[i].transform.gameObject.activeInHierarchy) {
                         
                         Debug.LogWarning(" >> comps[i].gameObject(" + comps[i].gameObject.name + ") is ACTIVE!!!");
                         collLst.Add(collTmp);
                     }
                     else {
                         Debug.LogWarning(" >> comps[i].gameObject(" + comps[i].gameObject.name + ") is deactive");
                     }
                     
                 }
             }
         }
         if(collLst != null && collLst.Count > 0) {
             bounds = collLst[0].bounds;
             if(collLst.Count > 1) {
                 for(i = 1; i < collLst.Count; i++) {
                     bounds.Encapsulate(collLst[i].bounds);
                 }
             }
             return (true);
         }
         return (false);
     }
And the script that create prefab is something like this:
 public class Container{
     //some variables
     public List<ElementsClass> elements;
     public void AddNewElement(GameObject prefab){
         GameObject goTemp = GameObject.Instantiate(prefab);
         // Do many things
         elements.AddNewElement(goTemp);
         elements[elements.Count - 1].goElement.SetActive(false);
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                