Child GameObjects in GameObject Problems (Best way to get Inactive Objects?)

TLDR: Are Child GameObjects a good way to track Inactive Objects, and how can I accomplish it?

Hi! So I have been having some more issues with programming and I wanted to ask a few questions.

1: Is using Child GameObjects the best way to find Inactive Objects?
2: If so; what is the best way to do so?

The reasons I ask is the following; I have a variable that lists all of a specific GameObject (Buttons). However, I wanted to also list Inactive Buttons as well. After looking it up online, I saw a great way: Child GameObjects. I made a ButtonManager Game Object, and made it so in the Button Script, it will automatically make itself a child to the ButtonManager Game Object. However, I have been having some problems doing so…

Currently; I have this script:

public GameObject ButtonGameObject;
private void Start()
        ButtonGameObject = this.gameObject;
        ButtonGameObject.transform.parent = LevelManager.GetComponentInChildren<ButtonManagerScript>().ButtonManagerGameObject.transform;

where the ButtonManager is a child of a LevelManager, and the Button is making itself a child of the LevelManager’s Child Object; the ButtonManager. However, this code to make buttons child objects is not working as intended.

What should happen (after all the code is done) is: at the start of a Level, all Buttons (Active or not) will become a child of the ButtonManager. A variable will then count how many Buttons are in the GameObject and set it as the number of buttons required to beat the level. If a Button is pressed, a second variable will increase by one. If the two variables equal each other, (those being the number of Buttons Pressed and the requirement) the ButtonManager will delete all Buttons in the GameObject and the next Level will start. I imagine this code could be improved, and I am not sure if Child Objects are what I should use to add Inactive GameObjects to the Manager.

Thanks for any help provided. :slight_smile:

The best way is one that integrates smoothly with your brain concept of a disabled GameObject as well as the design of your game.

In most cases, use The Unity Way™ and make a public reference:

public GameObject Thingy;

And then just turn it on and off everyplace you need to;

 Thingy.SetActive( activeOrNot);


Also, in your Start(), always assert what you want it to be, either on or off.

This gets you out of weird bugs where your code assumes it is OFF because you usually save the scene with it disabled, but once in a while you forget and save the scene with it enabled and now your code is backwards and fails.

So thinking about these scripts, I feel like I should have added way more context, as I realize I do have a reference. The exact code I have now is this;

public class LevelManager : MonoBehaviour
    // The amount of Buttons the Player pressed
    public int NumberOfButtonPress = 0;

    // The amount of Buttons in a Level (Including Disabled Buttons)
    public GameObject[] RequiredButtonPress;


    // Start is called before the first frame update
    void Start()
        RequiredButtonPress = GameObject.FindGameObjectsWithTag("Button");
        foreach (var ButtonScript in RequiredButtonPress)
            if (GetComponent<ButtonScript>().isButtonEnabled == false)

which I feel is okay; but the problem is it does not disable the buttons if I make it disabled
(For context, the way the script works is there is a bool on each button in a different script that if true, should make the button enabled. This script is the script that does the enabling and disabling. It is supposed to look for each Button’s variable that says whether or not it should be enabled, and enabling/disabling accordingly)

Also, I have been looking at some posts, including this: Getting the individual scripts of gameobjects within an array? and I don’t know if I even need an array to house more than one button for this purpose. I feel like I am close to the solution… but I don’t have it yet…

The other problem is I don’t know how to best disable the buttons in the RequiredButtonPress Array. I have it set so that after a level is completed, it checks. However, I need it to check for each scene change. Example why this is a problem; the way the system works, because there are no buttons in the Title Screen, it sets the value to 0. But it does not update that when loading into a level. Because there also is a failsafe that if the value is 0, the next level cannot start (I didn’t want to have to have a Button on the title screen instead, as that would still cause similar problems). So…

TLDR; How with the script above can I disable buttons/Why doesn’t this script work? Do I need an Array to even do this or am I making this script complicated? And how can I make the game check only once on how many buttons are in a level after EVERY Scene change?

Perhaps because you disable this script on line 19?

Decant line 17 into proper steps:

  • get the reference
  • test the property
  • do more things with it