Class GameObjectExtensions
Provides fluent extension methods on UnityEngine.GameObject that delegate directly to the static utility methods in GameObjectUtil.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class GameObjectExtensions
Remarks
Every method in this class is a thin forwarding wrapper; all logic, null-safety, and documentation live in the corresponding GameObjectUtil counterpart. These extensions allow call-site code to be written in a more object-oriented style, for example:
go.SetVisibility(false);
go.SetLayerRecursively(LayerMask.NameToLayer("UI"));
instead of:
GameObjectUtil.SetVisibility(go, false);
GameObjectUtil.SetLayerRecursively(go, LayerMask.NameToLayer("UI"));
Methods
EnableColliders(GameObject, bool, bool)
Enables or disables the UnityEngine.Collider.enabled flag on every 3D
UnityEngine.Collider and the Collider2D.enabled flag on every 2D
UnityEngine.Collider2D in the hierarchy rooted at obj.
Useful for temporarily disabling all physics interactions on a complex prefab.
No-op if obj is null.
Declaration
public static void EnableColliders(this GameObject gameObject, bool status, bool includeInactive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | status |
|
| bool | includeInactive | Whether colliders on inactive GameObjects are also affected. Defaults to |
GetBoundingBox(GameObject, bool)
Calculates and returns a world-space axis-aligned bounding box (AABB) for the hierarchy rooted at obj.
The method aggregates bounds from all colliders and renderers within the hierarchy. If no bounds are found, returns a default
(empty) UnityEngine.Bounds.
Declaration
public static Bounds GetBoundingBox(this GameObject gameObject, bool includeInactive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | includeInactive | Indicates whether to include inactive children in the bounds calculation. |
Returns
| Type | Description |
|---|---|
| Bounds | The calculated UnityEngine.Bounds representing the axis-aligned bounding box for the hierarchy. May be empty if no bounds are found. |
GetCentroid(GameObject, bool)
Returns the world-space centroid of the hierarchy rooted at obj.
When a bounding box can be computed from UnityEngine.Renderer or UnityEngine.Collider
components, the centroid is the center of that box. When no bounds are available,
falls back to obj.transform.position. Returns UnityEngine.Vector3.zero if
obj is null.
Declaration
public static Vector3 GetCentroid(this GameObject gameObject, bool includeInactive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | includeInactive | Whether inactive children are included when computing bounds.
Defaults to |
Returns
| Type | Description |
|---|---|
| Vector3 | The world-space center of the aggregated bounds, or the object's world position if no
bounds exist, or UnityEngine.Vector3.zero if |
GetChild(GameObject, string)
Retrieves the first direct child GameObject of the specified gameObject with the given
childName.
Returns null if no child with the specified name is found.
Declaration
public static GameObject GetChild(this GameObject gameObject, string childName)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | The parent GameObject to search. Must not be null. |
| string | childName | The name of the child GameObject to locate. Must not be null or empty. |
Returns
| Type | Description |
|---|---|
| GameObject | The child GameObject with the specified name, or null if not found. |
GetOrAddComponent<T>(GameObject)
Retrieves a component of type T from the specified obj or adds it to the
GameObject if it does not exist.
Declaration
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject |
Returns
| Type | Description |
|---|---|
| T | The existing or newly added component of type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of Component to retrieve or add. |
IsActiveInHierarchy(GameObject)
Returns true if obj is not null and its
UnityEngine.GameObject.activeInHierarchy flag is true, meaning both the
object itself and all of its ancestors are active.
Declaration
public static bool IsActiveInHierarchy(this GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject |
Returns
| Type | Description |
|---|---|
| bool |
|
IsActiveSelf(GameObject)
Returns true if obj is not null and its
UnityEngine.GameObject.activeSelf flag is true.
This reflects only the local activation state of the object itself and does not
indicate whether any parent is deactivated; use IsActiveInHierarchy(GameObject) for
a full hierarchy check.
Declaration
public static bool IsActiveSelf(this GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject |
Returns
| Type | Description |
|---|---|
| bool |
|
SetActiveRecursively(GameObject, bool, bool)
Calls SetActive(bool) on every node in the hierarchy
rooted at obj, using a stack-based depth-first traversal.
Unlike calling SetActive only on the root (which hides the subtree but does
not change child activeSelf values), this method individually marks every
descendant, ensuring predictable state when you later re-parent or move objects.
Declaration
public static void SetActiveRecursively(this GameObject gameObject, bool status, bool includeRoot = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | status |
|
| bool | includeRoot | Whether the root object itself is also affected. Defaults to |
SetActiveSafe(GameObject, bool)
Null-safe wrapper around SetActive(bool) that silently
ignores null references. Prefer this over a direct obj?.SetActive()
call when the reference may be a Unity-destroyed "fake null".
Declaration
public static void SetActiveSafe(this GameObject gameObject, bool status)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | status |
|
SetLayerRecursively(GameObject, int)
Assigns the specified Unity layer to every UnityEngine.GameObject in the
hierarchy rooted at obj, including the root itself. Uses a
stack-based depth-first traversal. No-op if obj is null.
Declaration
public static void SetLayerRecursively(this GameObject gameObject, int layer)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| int | layer | The layer index to assign (0-31). Use NameToLayer(string) to convert a layer name to its index. |
SetVisibility(GameObject, bool, bool)
Enables or disables the UnityEngine.Renderer.enabled flag on every
UnityEngine.Renderer component found in the hierarchy rooted at obj.
This controls whether each mesh/sprite is drawn without changing any activation state.
No-op if obj is null.
Declaration
public static void SetVisibility(this GameObject gameObject, bool status, bool includeInactive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| bool | status |
|
| bool | includeInactive | Whether renderers on inactive GameObjects are also affected. Defaults to |
TryGetBoundingBox(GameObject, out Bounds, bool)
Attempts to calculate a world-space axis-aligned bounding box (AABB) for the hierarchy rooted at the specified
obj.
Prefers bounds derived from Renderers; if none are found, falls back to bounds from 3D or 2D Colliders.
Declaration
public static bool TryGetBoundingBox(this GameObject gameObject, out Bounds bounds, bool includeInactive = true)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | |
| Bounds | bounds | Output parameter that will contain the calculated bounding box if the operation succeeds. |
| bool | includeInactive | Indicates whether inactive children should be included in the calculation. |
Returns
| Type | Description |
|---|---|
| bool | True if bounds were successfully computed from Renderers or Colliders; otherwise, false. |