Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved GetComponentsInChildren (but non-recursive)

Discussion in 'Scripting' started by SiMULOiD, Feb 19, 2022.

Thread Status:
Not open for further replies.
  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi,
    I’m trying to limit the below script to the first depth of child objects. I tried removing the GetComponentsInChildren<MeshAttachToObject>() and replace with gameObject.transform, but did not work. Any suggestions?


    Code (CSharp):
    1.        MeshAttachToObject[] BunchofObjects = GetComponentsInChildren<MeshAttachToObject>();
    2.         foreach (MeshAttachToObject AnObject in BunchofObjects)
    3.         {
    4.             if (AnObject.transform.parent != null)
    5.             {
    6.                 AnObject.GetComponent<MeshAttachToObject>().enabled = true;
    7.  
    8.             }
    9.         }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Didn't bother to read your code properly, but how about using transform.childCount and transform.GetChild()?

    AFAIK it should only get you direct children of some gameObject.

    Get those objects and then do a loop where you change/add/remove something for example from those objects?
     
    SiMULOiD likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,191
    The Transform class implements the IEnumerable interface, and it's been set up to let you iterate through all immediate children (non recursive) in a game object's transform by using the
    foreach
    syntax.

    So you can loop through all children in a GameObject like this:
    Code (CSharp):
    1. foreach (Transform transform in someGameObject.transform)
    2. {
    3.     //do ya thing
    4. }
     
  4. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thank you both very much for the suggestions.
    I ended up with the following, which skips over the first few children per my requirements. Appreciate your time and help.


    Code (CSharp):
    1. for (int childIndex = 3; childIndex < transform.childCount; ++childIndex)
    2.         {
    3.             Transform child = transform.GetChild(childIndex);
    4.             if (child.transform.parent != null)
    5.         {
    6.  
    7.                 child.GetComponent<MeshAttachToObject>().enabled = true;
    8.         }
    9.         }
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    What is the point of that if statement? You are iterating through the childs of another transform. So of course each child will have a parent, that's the whole point of iterating through the children...

    Also "child" is already of type Transform. Using the "transform" shortcut property on a transform reference makes no sense. Those are all the same

    Code (CSharp):
    1. child.parent
    2. child.transform.parent
    3. child.transform.transform.transform.transform.transform.parent
    When you do
    child.transform
    you essentially say: Here I have a transform reference (child). Please find the first Transform component on the gameobject where this transform is attached to. Since a gameobject can only have a single transform, the
    transform
    property would find the same component. So
    child == child.transform
    is always true when "child" is a Transform.
     
  6. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Yeah, that makes sense and is totally redundant.
    Thanks for the explanation.
     
  7. Kiryonn

    Kiryonn

    Joined:
    Oct 9, 2022
    Posts:
    1
    I'm a bit late but think of verifying that you do have a
    MeshAttachToObject
    on the child object.
    Something like:
    Code (CSharp):
    1. for (int childIndex = 3; childIndex < transform.childCount; childIndex++)
    2. {
    3.     Transform child = transform.GetChild(childIndex);
    4.     MeshAttachToObject meshAttachToObject = child.GetComponent<MeshAttachToObject>();
    5.     if(meshAttachToObject != null)
    6.         meshAttachToObject.enabled = true;
    7. }
    8.  
    If at some point you add childrens dinamically to your object, it ensures that you don't get a null reference exception.
     
Thread Status:
Not open for further replies.