Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Count Only active child's

Discussion in 'Scripting' started by Smaika, Dec 24, 2014.

  1. Smaika

    Smaika

    Joined:
    Apr 28, 2014
    Posts:
    14
    How can I count active child's and get a int
    transform.childCount will count all child's on the object I only want it to count active one
    so how can I do it
     
    Necronomicron likes this.
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    So, I imagine you are doing something like

    Code (csharp):
    1.  
    2. foreach(Transform child in transform){
    3. if(child.active)
    4. Count++;
    5. }
     
  3. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    If you're new to programming, it's a good example of using a category (or "extension" as they are called in c#)

    1. public static int ChildCountActive( this Transform t )
    2. {
    3. int k = 0;
    4. foreach(Transform c in t)
    5. {
    6. if(c.gameObject.activeSelf)
    7. k++;
    8. }
    9. return k;
    10. }
     
  4. freakadelle

    freakadelle

    Joined:
    Jan 9, 2018
    Posts:
    5
    I know this post is pretty old but I got another simple solution. Just try:

    GetComponentsInChildren<Transform>().GetLength(0);

    Now it will only count active children.

    Best regards
    Robert
     
  5. bkrbasant7

    bkrbasant7

    Joined:
    May 29, 2019
    Posts:
    1
    this works pretty well
    Thanks
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This works incorrect since it returns not only direct children, but all hierarchy recursive counting the object itself.
     
    rbitard and Kondziu2504 like this.
  7. DanielPaladino

    DanielPaladino

    Joined:
    Feb 1, 2020
    Posts:
    2
    Heh, this worked very well to me, thanks!
     
  8. serkantarakci

    serkantarakci

    Joined:
    May 24, 2020
    Posts:
    3
    Actually this works very well. Just find another component that there is only one in a child. I mean <Transform> is very common component and could be in every layers of a child. In my case I used <NavMeshAgent> and it worked perfectly.
     
  9. blazingPotato

    blazingPotato

    Joined:
    Feb 17, 2020
    Posts:
    5
    Thanks, Robert!

    For anyone like me who found this useful but doesn't quite understand how it works exactly, <T> is the name of the component. It can even be a name of a scrip attached to your game object.

    Example:
    If your gameObject has 10 child objects under them, this would get 10 transform components (count = 10 instead of 1).
    To avoid this, I'd get a single unique component (my bullet prefab has a "Bullet" script so I'd try
    Code (CSharp):
    1. GetComponentsInChildren<Bullet>().GetLength(0);
    Hope this helps!
     
    Dragontech97 likes this.