Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!