Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
How to check if any child of an object is active?

I have created weapons in my game and I made weapons not be active when it is taken but now Player can take 2 guns at the same time. I have added all my weapons to an empty object and I want to check if any child of object is active. All of the weapons have same script but but values of booleans are different. method is like that

void OnMouseDown()
    {
            if(weapon_is_taken == false)
            {
                weapon_is_taken = true;
            }
     }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There are multiple ways depending a bit on your needs.

To answer your title you could e.g. use

public bool IsAnyChildActive()
{
    // Iterates through all direct childs of this object
    foreach(Transform child in transform)
    {
        if(child.gameObject.activeSelf) return true;
    }

    return false;
}

However, depending on the amount of childs this is maybe a bit overhead every time.


I assume your goal is to be able to switch out the active weapon and immediately set all other weapons to inactive.

For this you could simply store the current active reference in your Weapon class like e.g.

public class Weapon : MonoBehaviour
{
    // Stores the reference to the currently active weapon
    private static Weapon currentlyActiveWeapon;

    // Read-only access from the outside
    // Only this class can change the value
    public static Weapon CurrentlyActiveWeapon
    {
        get => currentlyActiveWeapon;

        private set
        {
            if(currentlyActiveWeapon == value)
            {
                // Already the same reference -> nothing to do
                return;
            }

            // Is there a current weapon at all?
            if(currentlyActiveWeapon)
            {
                // Set the current weapon inactive
                currentlyActiveWeapon.gameObject.SetActive(false);
            }

            // Store the assigned value as the new active weapon
            currentlyActiveWeapon = value;
            // And set it active
            currentlyActiveWeapon.gameObject.SetActive(true);
        }
    }

    // Check if this is the currently active weapon
    public bool weapon_is_taken => currentlyActiveWeapon == this;

    public void SetThisWeaponActive()
    {
        CurrentlyActiveWeapon = this;
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

Gameobject in this context is a parent object that holds child objects (weapons)

  for (int i = 0; i < gameObject.transform.childCount; i++)
        {

            if (transform.GetChild(i).gameObject.activeSelf)
            {
                // do whatever you want if child is active
            }
            else
            {
                // do whatever you want if child is inactive

            }
        }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda