I'm trying to activate the array object that have button component.

 public GameObject[] weapon_carry_pistol = new GameObject[3];
 public int p1_carry,activeObjects;

 // Update is called once per frame
 void Update()

  p1_carry =  weapon_carry_pistol[0].GetComponentsInChildren<Button>().Length; // 6 objects

        for (int i = 0; i < activeObjects; i++)
        weapon_carry_pistol[0].GetComponentsInChildren<Button>()[i].enabled = true;


This script do activate the button component but not the whole game object. If I change "Button" to "GameObject" I get error //.GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.//


There is a conceptual difference between a GameObject and a Component. One GameObject has many Components and every Component belongs to a GameObject. If you have that thing on the UI which you consider a button, then what you actually have is a GameObject which has a Button component (among other components).

Some components can be enabled and disabled with a .enabled property or in some other way (not all components, though. For example, you can disable a Renderer but you can't disable a Transform). A GameObject on the other hand, is activated or deactivated with the SetActive(bool) method.

So if you want to get all the game objects in the children of weapon_carry_pistol[0] which have a button and then activate those game objects, then you:

  1. find all the button-components with GetComponentsInChildren. In this case you specifically want to include any buttons in inactive game objects, so you need to use GetComponentsInChildren<Button>(true).
  2. iterate those in a foreach loop
  3. determine the GameObject of the current button by checking the .gameObject property every component has.
  4. activate that GameObject with .SetActive(true)

Like this:

foreach (Button button in weapon_carry_pistol[0].GetComponentsInChildren<Button>(true) ) {
|improve this answer|||||
  • \$\begingroup\$ Thank you for your reply. Still something is missing. Here is the new [script now] (hastebin.com/yukepitaqo.cs). The issue now that when I increase "activeObjects" one value all objects with button active or not show up !! \$\endgroup\$ – user43474 Mar 9 '19 at 15:53
  • \$\begingroup\$ @user43474 I'm sorry, but I don't understand what you mean. Can you add more information about what exactly you want to do, how your object hierarchy looks? Why is weapon_carry_pistol an array anyway and what does activeObjectsmean? It's hard to tell what exactly you need when you don't show us the full picture. \$\endgroup\$ – Philipp Mar 10 '19 at 2:56
  • \$\begingroup\$ (i.postimg.cc/52KCnDqm/00.png) As you see on image, there is a parent and 6 child objects. In my script, "weapon_carry_pistol" is the parent object. That's why i'm using "GetComponentsInChildren" to get child objects inside my array and activate them by numbers. If activeObjects = 1. the first element in my array will be active. and so on. I tried "GameObject" instead "button" but I got error !! \$\endgroup\$ – user43474 Mar 10 '19 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.