Search Unity

Finding one active child gameobject

Discussion in 'Getting Started' started by Green11001, Jun 1, 2019.

  1. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Is there a simple way to find the one active child gameobject out of many unactive children?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Code (CSharp):
    1. GameObject activeChild = null;
    2.  
    3. foreach(Transform child in transform) {
    4.    if(child.gameObject.activeSelf) {
    5.       activeChild = child.gameObject;
    6.       break;
    7.    }
    8. }
    Note: This will only select the first active child object. If you have multiple active children and want to select a specific one, you'll need to identify it in some way.
     
    AkapsyJr and SomeC00lName like this.