Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
            }
     }
question from:https://stackoverflow.com/questions/66058697/how-to-check-if-any-child-of-an-object-is-active

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...