Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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

Discussion in 'Scripting' started by mrcipher, Sep 18, 2022.

  1. mrcipher

    mrcipher

    Joined:
    Jun 18, 2022
    Posts:
    7
    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:

    Code (CSharp):
    1. ...
    2. public GameObject ButtonGameObject;
    3. ...
    4. private void Start()
    5.     {
    6.         ButtonGameObject = this.gameObject;
    7.        
    8.         ButtonGameObject.transform.parent = LevelManager.GetComponentInChildren<ButtonManagerScript>().ButtonManagerGameObject.transform;
    9.     }
    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. :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,667
    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(tm) and make a public reference:

    Code (csharp):
    1. public GameObject Thingy;
    And then just turn it on and off everyplace you need to;

    Code (csharp):
    1.  Thingy.SetActive( activeOrNot);
    Done.

    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.
     
  3. mrcipher

    mrcipher

    Joined:
    Jun 18, 2022
    Posts:
    7
    Update:
    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;
    Code (CSharp):
    1. public class LevelManager : MonoBehaviour
    2. {
    3.     // The amount of Buttons the Player pressed
    4.     public int NumberOfButtonPress = 0;
    5.  
    6.     // The amount of Buttons in a Level (Including Disabled Buttons)
    7.     public GameObject[] RequiredButtonPress;
    8.  
    9.    ...
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {...
    14.         RequiredButtonPress = GameObject.FindGameObjectsWithTag("Button");
    15.         foreach (var ButtonScript in RequiredButtonPress)
    16.         {
    17.             if (GetComponent<ButtonScript>().isButtonEnabled == false)
    18.             {
    19.                 gameObject.SetActive(false);
    20.             }
    21.         }
    22.      
    23.     }
    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: https://forum.unity.com/threads/get...cripts-of-gameobjects-within-an-array.668512/ 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?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,667
    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