-
Notifications
You must be signed in to change notification settings - Fork 0
GameObject Extensions
This utility library, part of the GameObjectExtensions namespace, provides extension methods for Unity's GameObject class, offering additional functionality for common tasks.
Returns the object itself if it exists, null otherwise. 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 behaviour. The OrNull method use 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.
Object myObject = //someObject;
Object result = myObject.OrNull();Type Parameters:
-
T: The type of the object.
Parameters:
-
obj: The object being checked.
Returns:
-
T: The object itself if it exists and not destroyed, null otherwise.
Retrieves all the children of a given Transform.
IEnumerable<GameObject> children = gameObject.Children();Parameters:
-
parent: The Transform to retrieve children from.
Returns:
-
IEnumerable<GameObject>: An IEnumerable of all the child GameObjects of the parent.
Executes a specified action for each child of a given transform.
gameObject.ForEveryChild(child => Debug.Log(child.name));Parameters:
-
parent: The parent transform. -
action: The action to be performed on each child.
Immediately destroys all child game objects of the given transform.
gameObject.DestroyChildren();Parameters:
-
parent: The Transform whose child game objects are to be immediately destroyed.
Immediately destroys all child game objects of the given transform.
gameObject.DestroyChildrenImmediate();Parameters:
-
parent: The Transform whose child game objects are to be immediately destroyed.
Enables all child game objects of the given transform.
gameObject.EnableChildren();Parameters:
-
parent: The Transform whose child game objects are to be enabled.
Disables all child game objects of the given transform.
gameObject.DisableChildren();Parameters:
-
parent: The Transform whose child game objects are to be disabled.
Checks if the GameObject is a prefab.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool isPrefab = myObject.IsPrefab();
Console.WriteLine($"Is the GameObject a prefab? {isPrefab}");Parameters:
-
gameObject(GameObject): The GameObject to check.
Returns:
- bool: True if the GameObject is a prefab, false otherwise.
Checks if the GameObject has a specified component type.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool hasRigidbody = myObject.HasComponent<Rigidbody>();
Console.WriteLine($"Does the GameObject have a Rigidbody component? {hasRigidbody}");Type Parameters:
-
T: The type of component to check for.
Parameters:
-
gameObject(GameObject): The GameObject to check.
Returns:
- bool: True if the GameObject has the specified component type, false otherwise.
Checks if the GameObject has a Rigidbody component.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool hasRigidbody = myObject.HasRigidbody();
Console.WriteLine($"Does the GameObject have a Rigidbody component? {hasRigidbody}");Parameters:
-
gameObject(GameObject): The GameObject to check.
Returns:
- bool: True if the GameObject has a Rigidbody component, false otherwise.
Checks if the GameObject has an Animation component.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool hasAnimation = myObject.HasAnimation();
Console.WriteLine($"Does the GameObject have an Animation component? {hasAnimation}");Parameters:
-
gameObject(GameObject): The GameObject to check.
Returns:
- bool: True if the GameObject has an Animation component, false otherwise.
Checks if the GameObject has an Animator component.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool hasAnimator = myObject.HasAnimator();
Console.WriteLine($"Does the GameObject have an Animator component? {hasAnimator}");Parameters:
-
gameObject(GameObject): The GameObject to check.
Returns:
- bool: True if the GameObject has an Animator component, false otherwise.
Attempts to get a component of type T from the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Rigidbody rigidbodyComponent;
bool success = myObject.TryGetComponent(out rigidbodyComponent);
Console.WriteLine($"Did the attempt to get the Rigidbody component succeed? {success}");Type Parameters:
-
T: The type of component to get.
Parameters:
-
gameObject(GameObject): The GameObject to get the component from. -
outComponent(out T): The resulting component if found.
Returns:
- bool: True if the component is found, false otherwise.
Attempts to get a component of type T from the GameObject or its ancestors.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Rigidbody rigidbodyComponent;
bool success = myObject.TryGetComponentInParent(out rigidbodyComponent);
Console.WriteLine($"Did the attempt to get the Rigidbody component in parent succeed? {success}");Type Parameters:
-
T: The type of component to get.
Parameters:
-
gameObject(GameObject): The GameObject to search. -
outComponent(out T): The resulting component if found.
Returns:
- bool: True if the component is found, false otherwise.
Attempts to get a component of type T from the GameObject or its descendants.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Rigidbody rigidbodyComponent;
bool success = myObject.TryGetComponentInChildren(out rigidbodyComponent);
Console.WriteLine($"Did the attempt to get the Rigidbody component in children succeed? {success}");Type Parameters:
-
T: The type of component to get.
Parameters:
-
gameObject(GameObject): The GameObject to search. -
outComponent(out T): The resulting component if found.
Returns:
- bool: True if the component is found, false otherwise.
Gets or adds a component of type T to the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
SomeComponentType component = myObject.GetOrAddComponent<SomeComponentType>();
Console.WriteLine($"Got or added the component: {component}");Type Parameters:
-
T: The type of component to get or add.
Parameters:
-
gameObject(GameObject): The GameObject to get or add the component to.
Returns:
- T: The resulting component.
Gets or adds a component of the specified type to the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Type someComponentType = typeof(SomeComponentType);
Component component = myObject.GetOrAddComponent(someComponentType);
Console.WriteLine($"Got or added the component: {component}");Parameters:
-
gameObject(GameObject): The GameObject to get or add the component to. -
type(Type): The type of component to get or add.
Returns:
- Component: The resulting component.
Destroys a component of type T on the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool success = myObject.DestroyComponent<SomeComponentType>();
Console.WriteLine($"Did the attempt to destroy the component succeed? {success}");Type Parameters:
-
T: The type of component to destroy.
Parameters:
-
gameObject(GameObject): The GameObject to destroy the component on.
Returns:
- bool: True if the component was destroyed, false otherwise.
Destroys a component of the specified type on the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Type someComponentType = typeof(SomeComponentType);
bool success = myObject.DestroyComponent(someComponentType);
Console.WriteLine($"Did the attempt to destroy the component succeed? {success}");Parameters:
-
gameObject(GameObject): The GameObject to destroy the component on. -
type(Type): The type of component to destroy.
Returns:
- bool: True if the component was destroyed, false otherwise.
Searches for a component of type T with the specified name in the GameObject's descendants.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
string componentName = "SomeComponent";
SomeComponentType component = myObject.SearchComponent<SomeComponentType>(componentName);
Console.WriteLine($"Found the component: {component}");Type Parameters:
-
T: The type of component to search for.
Parameters:
-
gameObject(GameObject): The GameObject
to search.
-
searchName(string): The name of the component to search for.
Returns:
- T: The first matching component, or null if not found.
Finds a component of type T in the GameObject's ancestors.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
SomeComponentType component = myObject.FindComponentInParents<SomeComponentType>();
Console.WriteLine($"Found the component in parents: {component}");Type Parameters:
-
T: The type of component to find.
Parameters:
-
gameObject(GameObject): The GameObject to search.
Returns:
- T: The first matching component, or null if not found.
Finds a component of the specified type in the GameObject's ancestors.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Type someComponentType = typeof(SomeComponentType);
Component component = myObject.FindComponentInParents(someComponentType);
Console.WriteLine($"Found the component in parents: {component}");Parameters:
-
gameObject(GameObject): The GameObject to search. -
type(Type): The type of component to find.
Returns:
- Component: The first matching component, or null if not found.
Creates child GameObjects based on the provided path name.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
string pathName = "Child1/Child2/Child3";
char splitChar = '/';
GameObject[] createdChildren = myObject.CreateChild(pathName, splitChar);
Console.WriteLine($"Created {createdChildren.Length} child GameObjects.");Parameters:
-
gameObject(GameObject): The parent GameObject. -
pathName(string): The path name to create child GameObjects. -
splitChar(char): The character used to split the pathName (default is '/').
Returns:
- GameObject[]: An array of created child GameObjects.
Gets the path of the GameObject in the scene hierarchy.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
string path = myObject.Path();
Console.WriteLine($"Path of the GameObject: {path}");Parameters:
-
gameObject(GameObject): The GameObject to get the path for.
Returns:
- string: The path of the GameObject in the scene hierarchy.
Gets the full path of the GameObject in the scene hierarchy, including the GameObject's name.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
string fullPath = myObject.PathFull();
Console.WriteLine($"Full path of the GameObject: {fullPath}");Parameters:
-
gameObject(GameObject): The GameObject to get the full path for.
Returns:
- string: The full path of the GameObject in the scene hierarchy.
Gets the root GameObject in the hierarchy of the provided GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
GameObject root = myObject.Root();
Console.WriteLine($"Root GameObject: {root}");Parameters:
-
go(GameObject): The GameObject to find the root for.
Returns:
- GameObject: The root GameObject in the hierarchy.
Gets the depth of the GameObject in the hierarchy.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
int depth = myObject.Depth();
Console.WriteLine($"Depth of the GameObject: {depth}");Parameters:
-
go(GameObject): The GameObject to find the depth for.
Returns:
- int: The depth of the GameObject in the hierarchy.
Checks if the GameObject's layer is included in a specific layer index.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool isInLayer = myObject.ContainLayer(8); // Check against layer index
Debug.Log($"Is the GameObject in layer 8? {isInLayer}");Parameters:
-
gameObject(GameObject): The GameObject to check. -
otherLayerIndex(int): The layer index to check against.
Returns:
- bool: True if the GameObject's layer is included, false otherwise.
Checks if the GameObject's layer is included in a specific LayerMask.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool isInLayerMask = myObject.ContainLayer(LayerMask.GetMask("SomeLayer")); // Check against LayerMask
Debug.Log($"Is the GameObject in SomeLayer? {isInLayerMask}");Parameters:
-
gameObject(GameObject): The GameObject to check. -
otherLayerMask(LayerMask): The LayerMask to check against.
Returns:
- bool: True if the GameObject's layer is included, false otherwise.
Checks if the GameObject's layer is included in all specified LayerMasks.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool isInAllLayers = myObject.ContainLayers(LayerMask.GetMask("Layer1"), LayerMask.GetMask("Layer2"));
Debug.Log($"Is the GameObject in both Layer1 and Layer2? {isInAllLayers}");Parameters:
-
gameObject(GameObject): The GameObject to check. -
otherLayerMasks(params LayerMask[]): The array of LayerMasks to check against.
Returns:
- bool: True if the GameObject's layer is included in all specified LayerMasks, false otherwise.
Checks if the GameObject's layer is included in at least one of the specified LayerMasks.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
bool isInOneOfLayers = myObject.ContainOneOfLayers(LayerMask.GetMask("Layer1"), LayerMask.GetMask("Layer2"));
Debug.Log($"Is the GameObject in either Layer1 or Layer2? {isInOneOfLayers}");Parameters:
-
gameObject(GameObject): The GameObject to check. -
otherLayerMasks(params LayerMask[]): The array of LayerMasks to check against.
Returns:
- bool: True if the GameObject's layer is included in at least one of the specified LayerMasks, false otherwise.
Sets the layer of the GameObject using the provided LayerMask.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
myObject.SetLayer(LayerMask.GetMask("NewLayer"));
Debug.Log($"Set the GameObject to NewLayer.");Parameters:
-
gameObject(GameObject): The GameObject to set the layer for. -
layerMask(LayerMask): The LayerMask specifying the new layer.
Sets the layer of the GameObject and its children using the provided LayerMask.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
myObject.SetLayerRecursion(LayerMask.GetMask("NewLayer"));
Debug.Log($"Set the GameObject and its children to NewLayer.");Parameters:
-
gameObject(GameObject): The GameObject to set the layer for. -
layerMask(LayerMask): The LayerMask specifying the new layer.
Sets the tag of the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
myObject.SetTag("NewTag");
Debug.Log($"Set the tag of the GameObject to NewTag.");Parameters:
-
gameObject(GameObject): The GameObject to set the tag for. -
tag(string): The new tag.
Sets the tag of the GameObject and its children.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
myObject.SetTagRecursion("NewTag");
Debug.Log($"Set the tag of the GameObject and its children to NewTag.");Parameters:
-
gameObject(GameObject): The GameObject to set the tag for. -
tag(string): The new tag.
Gets the duration of the ParticleSystem on the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
float particleDuration = myObject.GetParticleDuration();
Debug.Log($"Particle duration of the GameObject: {particleDuration} seconds");Parameters:
-
gameObject(GameObject): The GameObject with the ParticleSystem. -
includeChildren(bool): Include ParticleSystems in children. -
includeInactive(bool): Include inactive ParticleSystems. -
allowLoop(bool): Consider looping duration.
Returns:
- float: The duration of the ParticleSystem(s).
Gets the maximum time of TrailRenderer(s) on the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
float trailRendererTime = myObject.GetTrailRendererTime();
Debug.Log($"Maximum time of TrailRenderer(s): {trailRendererTime} seconds");Parameters:
-
gameObject(GameObject): The GameObject with TrailRenderer(s).
Returns:
- float: The maximum time of TrailRenderer(s).
Gets the bounding box of the GameObject.
// Example usage
GameObject myObject = /* Instantiate or reference a GameObject */;
Bounds objectBounds = myObject.GetBounds();
Debug.Log($"Bounding box of the GameObject: {objectBounds}");Parameters:
-
gameObject(GameObject): The GameObject to get the bounds for. -
includeChildren(bool): Include bounds of children.
Returns:
- Bounds: The bounding box of the GameObject.