Issue with Button Click Event Not Showing All Child Objects in Unity

Hello Unity Community,

I am encountering an issue in my Unity project where a button click event is not functioning as expected. The goal is to have a “BackButton” reveal all child objects of a parent GameObject regardless of their prior visibility states set by other buttons in the scene.

Project Setup:

  1. Parent Object ("Skull"): Contains two child objects - "Facial Bones" and "Cranial Bones"
  2. In "Facial Bones", there is many other child object like “ZygomaticBones”, “MaxillaBone”, “MadibullaBone” etc same thing with “Cranial Bones”.
  3. Buttons: "FacialButton", "CranialButton", “ZygoButton”, “MaxillaButton”, ”MadibullaButton”, etc

Skull
├── +Facial Bones
│ ├── ZygomaticBones
│ └── ...
└── +Cranial Bones
├── ZygomaticBones
└── ...


Clicking `"FacialButton"` or `"CranialButton"` shows only their respective object and button then hides others. Like if you click `“Facial Button”` it will show all object inside `“Facial Bones”` also `“ZygoButton”`, `“MaxillaButton”`, `”MadibullaButton”`, `“BackButton”`, etc and hide other object and button. Then after that if you click `“ZygoButton”` or `“MaxillaButton”` or `”MadibullaButton”`, it only show  their respective object and hide others. And for `"BackButton"` it should make all objects in `“Facial Bones”` visible again.

### Scripts:
- `ShowOneObjectOnly` Script (Attached to `"“ZygoButton”`, `“MaxillaButton”`, `”MadibullaButton"`):

This script shows only one child object of `"Skull"` and hides others when a button is clicked.

```cs
void ShowObjectOnly()
{
    if (parentObject != null && objectToShow != null)
    {
        foreach (Transform child in parentObject.transform)
        {
            child.gameObject.SetActive(child.gameObject == objectToShow);
        }
    }
    else
    {
        Debug.LogWarning("ParentObject or ObjectToShow is not assigned. Please drag and drop the GameObjects.");
    }
}

public static void ResetVisibility(GameObject parentObject)
{
    foreach (Transform child in parentObject.transform)
    {
        child.gameObject.SetActive(true);
    }
}
  • ShowAllObjects Script (Attached to “BackButton”):

Intended to show all child objects when “BackButton” is clicked.

void ShowAll()
{
    if (parentObject != null)
    {
        foreach (Transform child in parentObject.transform)
        {
            child.gameObject.SetActive(true);
        }
    }
    else
    {
        Debug.LogError("ParentObject is not assigned.");
    }
}

Issue:

When I click the "BackButton", it doesn’t show all the child objects as expected. It seems to maintain the visibility state set by the last clicked button

Question:

  1. What could be preventing the "BackButton" from correctly setting all child objects to active?
  2. Are there any known conflicts or issues with button click events and GameObject visibility in Unity that I should be aware of?

I appreciate any insights or suggestions you can offer to resolve this issue. Thank you!

If you have an animation controller which controls the “active” flag in any of the objects in the list, you may not be able to programmatically modify the active state of that particular object since it’ll be overwritten by the animator controller on every frame.

Another issue may simply be the tree hierarchy of your objects. Did you assign the parent object correctly? You can always add a parameter to your debug logs so that when you click them, the script/object that triggered get selected in the Hierarchy pane for easier debugging

// add 'this' when calling debug to add reference to the object invoking the message
           Debug.LogError("Some nasty error!", this);

One thing I noticed after debugging your code… Your “Show All” method is only being applied to the root object and their children (assuming your parentObject is the root of the tree hierarchy).

You probably want to Implement something that recursively sets the “active” flag back to true to all the transforms under the parentObject, like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowEverything : MonoBehaviour
{
    public GameObject parentObject;
    public void ShowAll()
    {
        if (parentObject != null)
        {
            ShowChildren(parentObject.transform);
        }
        else
        {
            Debug.LogError("ParentObject is not assigned.", this);
        }
    }

    void ShowChildren(Transform root)
    {
        if (root.childCount == 0)
        {
            Debug.Log($"Showing {root.gameObject.name}", root.gameObject);
            root.gameObject.SetActive(true);
            return;
        }

        foreach (Transform child in root)
        {
            ShowChildren(child);
        }
    }
}