UGTK / Toolkit / Libraries Extensions / GameObject Extension Completed
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
- Generic Component:
required to compile —
GetUGTKComponent,GetUGTKComponentsandHasUGTKComponentare typed onMn_UGTKComponent.
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.FindChildByNamesearches withGetComponentsInChildren<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
SetActiveRecursivelyis 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 overGetChildrendOfChildren().
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'sGetComponentsInChildren<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>();
GetUGTKComponentlogsDebug.LogError("Component not found")when it finds nothing — andHasUGTKComponentcalls it, so a perfectly legitimateif (go.HasUGTKComponent<T>())that returns false still prints an error in the console. Use it knowing that, or check withGetUGTKComponents<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:
Root → Branch → Leaf A, Leaf B, plus Root → Deep → Hidden. 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:
- List Children — two children,
Leaf AandLeaf B. Only the direct ones:Hiddenis a grandchild ofRootand does not appear. - Show Root —
GetRootGameObject()climbs toRoot, however deep the target sits. - Find Child —
FindChildByName("Leaf B")searches the whole subtree. Type a name that does not exist and it returnsnullwithout raising anything, which is the behaviour worth knowing. - Check Is Child Of —
Branchis reported as a descendant ofRoot. - Deactivate Branch, then Activate Branch —
SetActiveRecursivelywalks the whole subtree, not just the object it is called on. - Destroy Children — refused, because Allow Destruction is off. Turn it on and the same
entry really does remove every child:
DestroyAllChildrenon 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
Depends on 1
Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 1 module in total.
UGTKengine within an engine
