UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Libraries Extensions / GameObject Extension Completed

Libraries Extensions — UGTK

GameObject Extension

Summary

The GameObject Extension adds to GameObject the helpers you end up writing in every project: list the children, find a child by name, destroy them all, toggle the active state, walk up to the root — plus the UGTK-specific lookups for Mn_UGTKComponent.

Every method is a C# extension method on GameObject, so it reads as if it were part of Unity.


Content


Modules Dependencies


Setup

Nothing to set up: no component, no prefab, no scene — so there is nothing to show in a GIF. Add UGTK.LibrariesExtensions.GameObjectExtension to your assembly definition references and:

using UGTK.LibrariesExtensions.GameObjectExtension;

The class is GameObjectExtension**s** (plural) — but since every method is an extension method, you rarely type the class name at all.


How To Use

Children

List<GameObject> kids     = go.GetChildren();              // DIRECT children only, one level
List<GameObject> everyone = go.GetChildrendOfChildren();   // the whole subtree, recursive
GameObject found          = go.FindChildByName("Handle");  // recursive search by name

GetChildren() is one level deep; GetChildrendOfChildren() (the typo is in the API) is the recursive one. FindChildByName searches with GetComponentsInChildren<Transform>(), which includes the object itself: calling it with the object's own name returns the object, not a child.

Destroying

go.DestroyAllChildren();            // Destroy: at the end of the frame, use at runtime
go.DestroyImmediateAllChildren();   // DestroyImmediate: EDITOR ONLY

Both act on direct children only. DestroyImmediateAllChildren must not be called from game code: DestroyImmediate is an editor API.

Active state

go.ToggleActiveState();             // active <-> inactive
go.SetActiveRecursively(true);      // see the caveat below

SetActiveRecursively is not recursive. It sets the object and its direct children, and stops there — grandchildren keep their own state. If you need the whole subtree, loop over GetChildrendOfChildren().

Components

bool hasImage = go.HasComponent<Image>();                  // on the object itself
List<Image> imagesInKids = go.GetAllTypeComponents<Image>();  // one per DIRECT child that has it

GetAllTypeComponents<T>() looks at the direct children, not at the object itself and not at grandchildren — despite what the name suggests. For the classic behaviour use Unity's GetComponentsInChildren<T>().

UGTK components

var comp  = go.GetUGTKComponent<Mn_MyComponent>();     // first direct child that has it
var comps = go.GetUGTKComponents<Mn_MyComponent>();    // all direct children that have it
bool has  = go.HasUGTKComponent<Mn_MyComponent>();

GetUGTKComponent logs Debug.LogError("Component not found") when it finds nothing — and HasUGTKComponent calls it, so a perfectly legitimate if (go.HasUGTKComponent<T>()) that returns false still prints an error in the console. Use it knowing that, or check with GetUGTKComponents<T>().Count > 0.

Hierarchy

GameObject root = go.GetRootGameObject();   // transform.root
bool isChild    = go.IsChildOf(parent);     // true also when go == parent (Unity semantics)

Testing Scene

Open Debug/GameObjectExtensionScene.unity. It holds GameObjectExtensionTest, carrying Mn_GameObjectExtensionTest, and a small hierarchy to run the helpers against: RootBranchLeaf A, Leaf B, plus RootDeepHidden. Every method is on the component's context menu: right-click the header in the Inspector, with no play mode.

The component starts with Target set to Branch, Child Name to Leaf B and Parent Candidate to Root. What to do, in order:

  1. List Children — two children, Leaf A and Leaf B. Only the direct ones: Hidden is a grandchild of Root and does not appear.
  2. Show RootGetRootGameObject() climbs to Root, however deep the target sits.
  3. Find ChildFindChildByName("Leaf B") searches the whole subtree. Type a name that does not exist and it returns null without raising anything, which is the behaviour worth knowing.
  4. Check Is Child OfBranch is reported as a descendant of Root.
  5. Deactivate Branch, then Activate BranchSetActiveRecursively walks the whole subtree, not just the object it is called on.
  6. Destroy Children — refused, because Allow Destruction is off. Turn it on and the same entry really does remove every child: DestroyAllChildren on a scene object is as permanent as it sounds, which is why the flag exists.

Technical Info

Path Content
Scripts/GameObjectExtension.cs The whole system: the static class GameObjectExtensions

API: GetAllTypeComponents<T>(), GetUGTKComponents<T>(), GetUGTKComponent<T>(), HasUGTKComponent<T>(), GetChildren(), GetChildrendOfChildren(), HasComponent<T>(), DestroyAllChildren(), DestroyImmediateAllChildren(), FindChildByName(string), SetActiveRecursively(bool), GetRootGameObject(), IsChildOf(GameObject), ToggleActiveState().


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 1 module in total.