GameObject Extensions
Get Or Add Component
This retrieves an existing component of type T or adds it if it doesn't exist.
GameObject myObject = new GameObject();
Rigidbody rb = myObject.GetOrAddComponent<Rigidbody>();
Reset Transform
This resets the transform of a GameObject to zero position, no rotation, and a scale of one.
Transform myTransform = myObject.transform;
myTransform.ResetTransform();
Is Grounded
This checks if the GameObject is grounded using a raycast.
bool grounded = myObject.Grounded(0.1f, LayerMask.GetMask("Ground"));
Add Force Towards
This applies force to a Rigidbody in the direction of another GameObject.
Rigidbody rb = myObject.GetComponent<Rigidbody>();
rb.AddForceTowards(target, 10f);
Set Velocity Towards
This sets the Rigidbody's velocity towards a specified target position at a given speed.
Rigidbody rb = myObject.GetComponent<Rigidbody>();
rb.SetVelocityTowards(targetPosition, 5f);
Get Closest
This method is especially useful in situations like AI targeting, where you need to find the nearest point of interest (enemies, items, etc.) to a specific GameObject.
GameObject[] enemies;
GameObject closestEnemy = gameObject.GetClosest(enemies);
Is Visible To Camera
This checks whether the GameObject is currently visible by any camera.
Camera mainCamera = Camera.main;
bool isVisible = myObject.VisibleToCamera(mainCamera);
Set Layer
This sets the layer of a GameObject using its name.
GameObject myObject = new GameObject();
myObject.SetLayer("Player");
Has Layer
This checks if a GameObject's layer matches a specified layer name.
GameObject myObject = new GameObject();
bool hasLayer = myObject.HasLayer("Player");
Set Tag
This sets the tag of a GameObject.
GameObject myObject = new GameObject();
myObject.SetTag("Enemy");
Has Tag
This checks if a GameObject's tag matches a specified tag name.
GameObject myObject = new GameObject();
bool hasTag = myObject.HasTag("Enemy");
Add Child
This adds a child GameObject to a parent GameObject.
GameObject childObject = new GameObject("Child");
myObject.AddChild(childObject);
Clear Children
This removes all child GameObjects from a parent GameObject.
GameObject myObject = new GameObject();
myObject.ClearChildren();
Get Child By Name
This retrieves a child GameObject by its name.
GameObject myObject = new GameObject();
GameObject child = myObject.GetChildByName("Child");
Get Child By Tag
This retrieves a child GameObject by its tag.
GameObject myObject = new GameObject();
GameObject taggedChild = myObject.GetChildByTag("Enemy");
Last updated