Table of Contents

Class

Namespace
CoreFramework

Provides extension methods for Unity GameObjects to simplify common operations such as managing components, transformations, hierarchy, and more.

public static class GameObjectExtensions
Inheritance
object

Methods

DestroyChildren(GameObject)

Destroys all children of the game object

public static void DestroyChildren(this GameObject gameObject)

Parameters

gameObject GameObject

GameObject, whose children are to be destroyed.

DestroyChildrenImmediate(GameObject)

Immediately destroys all children of the given GameObject.

public static void DestroyChildrenImmediate(this GameObject gameObject)

Parameters

gameObject GameObject

GameObject, whose children are to be destroyed.

DisableChildren(GameObject)

Disables all child GameObjects associated with the given GameObject.

public static void DisableChildren(this GameObject gameObject)

Parameters

gameObject GameObject

GameObject, whose child GameObjects are to be disabled.

EnableChildren(GameObject)

Enables all child GameObjects associated with the given GameObject.

public static void EnableChildren(this GameObject gameObject)

Parameters

gameObject GameObject

GameObject, whose child GameObjects are to be enabled.

GetOrAdd<T>(GameObject)

Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one.

public static T GetOrAdd<T>(this GameObject gameObject) where T : Component

Parameters

gameObject GameObject

The GameObject to get the component from or add the component to.

Returns

T

The existing component of the given type, or a new one if no such component exists.

Type Parameters

T

The type of the component to get or add.

Remarks

This method is useful when you don't know if a GameObject has a specific type of component, but you want to work with that component regardless. Instead of checking and adding the component manually, you can use this method to do both operations in one line.

HideInHierarchy(GameObject)

This method is used to hide the GameObject in the Hierarchy view.

public static void HideInHierarchy(this GameObject gameObject)

Parameters

gameObject GameObject

OrNull<T>(T)

Returns the object itself if it exists, null otherwise.

public static T OrNull<T>(this T obj) where T : Object

Parameters

obj T

The object being checked.

Returns

T

The object itself if it exists and not destroyed, null otherwise.

Type Parameters

T

The type of the object.

Remarks

This method helps differentiate between a null reference and a destroyed Unity object. Unity's “== null” check can incorrectly return true for destroyed objects, leading to misleading behavior. The OrNull method uses Unity's “null check”, and if the object has been marked for destruction, it ensures an actual null reference is returned, aiding in correctly chaining operations and preventing NullReferenceExceptions.

Path(GameObject)

Returns the hierarchical path in the Unity scene hierarchy for this GameObject.

public static string Path(this GameObject gameObject)

Parameters

gameObject GameObject

The GameObject to get the path for.

Returns

string

A string representing the full hierarchical path of this GameObject in the Unity scene. This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending with the name of the specified GameObjects parent.

PathFull(GameObject)

Returns the full hierarchical path in the Unity scene hierarchy for this GameObject.

public static string PathFull(this GameObject gameObject)

Parameters

gameObject GameObject

The GameObject to get the path for.

Returns

string

A string representing the full hierarchical path of this GameObject in the Unity scene. This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending with the name of the specified GameObject itself.

ResetTransformation(GameObject)

Resets the GameObject's transform's position, rotation, and scale to their default values.

public static void ResetTransformation(this GameObject gameObject)

Parameters

gameObject GameObject

GameObject, whose transformation is to be reset.

SetActive<T>(T)

Activates the GameObject associated with the MonoBehaviour and returns the instance.

public static T SetActive<T>(this T obj) where T : MonoBehaviour

Parameters

obj T

The MonoBehaviour whose GameObject will be activated.

Returns

T

The instance of the MonoBehaviour.

Type Parameters

T

The type of the MonoBehaviour.

SetInactive<T>(T)

Deactivates the GameObject associated with the MonoBehaviour and returns the instance.

public static T SetInactive<T>(this T obj) where T : MonoBehaviour

Parameters

obj T

The MonoBehaviour whose GameObject will be deactivated.

Returns

T

The instance of the MonoBehaviour.

Type Parameters

T

The type of the MonoBehaviour.

SetLayersRecursively(GameObject, int)

Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy.

public static void SetLayersRecursively(this GameObject gameObject, int layer)

Parameters

gameObject GameObject

The GameObject to set layers for.

layer int

The layer number to set for GameObject and all of its descendants.