# OnTransformChildrenChanged()

> Called when the list of direct children of a GameObject's Transform changes.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.3/script-reference/unityengine.md)

```csharp
public void OnTransformChildrenChanged()
```

### Remarks

The following changes to direct children of a GameObject invoke `OnTransformChildrenChanged`: - Adding a new child object to the GameObject's Transform. - Removing a child object. - Moving a child to a different parent. - Changing the order of the direct children. It makes no difference whether these actions are performed in the Hierarchy window or from code with APIs such as [Transform.SetParent](/engine/6000.3/script-reference/unityengine/transform/setparent.md), [Object.Destroy](/engine/6000.3/script-reference/unityengine/object/destroy.md), or [Transform.SetSiblingIndex](/engine/6000.3/script-reference/unityengine/transform/setsiblingindex.md). `OnTransformChildrenChanged` is only triggered by changes to the immediate (direct) children of the Transform. Changes to grandchildren or deeper descendants do not invoke this method on the parent. `OnTransformChildrenChanged` is called at runtime when a GameObject's direct children change. It can also be called in Edit mode if scripts use [ExecuteInEditMode](/engine/6000.3/script-reference/unityengine/executeineditmode.md) or [ExecuteAlways](/engine/6000.3/script-reference/unityengine/executealways.md). Use this callback to respond to changes in your GameObject's immediate child hierarchy, such as updating UI elements, managing child object lists, or refreshing cached references. Additional Resources: [MonoBehaviour.OnTransformParentChanged()](/engine/6000.3/script-reference/unityengine/monobehaviour/ontransformparentchanged.md).

### Examples

```csharp

// Attach this script to any GameObject (for example, an empty "Parent" object).
// Assign a UI Text component to the countText field in the Inspector.
// Add or remove children from the GameObject at runtime or in the Editor.

using UnityEngine;
using UnityEngine.UI;

public class ChildrenCounter : MonoBehaviour
{
    public Text countText;

    private void Start()
    {
        UpdateChildrenCount();
    }

    // Called by Unity when a child is added or removed
    private void OnTransformChildrenChanged()
    {
        UpdateChildrenCount();
    }

    private void UpdateChildrenCount()
    {
        int childrenCount = transform.childCount;
        if (countText != null)
        {
            countText.text = $"Children: {childrenCount}";
        }
    }
}
```
