| Type | Class (sealed) |
|---|---|
| Namespace | UnityEngine |
| Inherits | Object |
| In scripts | gameObject (the one your script is on) |
GameObject
A GameObject is any object in your scene. On its own it's an empty container with a name and a Transform. You make it do things by adding components.
Inside a MonoBehaviour, gameObject (lowercase) is the GameObject the script is attached to. GameObject (uppercase) is the class, used for static functions like GameObject.Find.
Properties
SetActive(false) is unplugging an appliance. It's still in the room (in the Hierarchy), keeps its settings, and you can plug it back in anytime. Unplugging a power strip (a parent) turns off everything plugged into it (the children), even if each device's own switch (activeSelf) is still on.
| Name | Type | Description |
|---|---|---|
name | string | Name shown in the Hierarchy. |
transform | Transform | Its position, rotation and scale. Every GameObject has one. |
activeSelf | bool | Is this object's own checkbox ticked? (Read only. Use SetActive to change.) |
activeInHierarchy | bool | Is it actually active? False if it or any parent is inactive. |
tag | string | A label like "Player" or "Enemy". Add tags in the Tags & Layers settings. |
layer | int | Layer number (0-31). Used for physics filtering and camera culling. |
scene | Scene | The scene it belongs to. |
Methods
| Method | Returns | Description |
|---|---|---|
SetActive(bool value) | void | Turns the object (and all its children) on or off. Off means no rendering, no physics, no Update. |
GetComponent<T>() | T | Gets a component of type T on this object, or null if there isn't one. |
TryGetComponent<T>(out T component) | bool | Like GetComponent but returns true/false. Cleaner when the component might be missing. |
GetComponentInChildren<T>() | T | Searches this object, then its children (depth-first). |
GetComponentInParent<T>() | T | Searches this object, then its parents. |
GetComponents<T>() | T[] | All components of type T on this object. |
AddComponent<T>() | T | Adds a new component and returns it. Its Awake runs immediately. |
CompareTag(string tag) | bool | Checks the tag. Faster than tag == "Player". |
Static Methods
| Method | Returns | Description |
|---|---|---|
GameObject.Find(string name) | GameObject | Finds an active object by name. Slow; avoid in Update. |
GameObject.FindWithTag(string tag) | GameObject | Finds one active object with the tag. |
GameObject.FindGameObjectsWithTag(string tag) | GameObject[] | Finds all active objects with the tag. |
GameObject.CreatePrimitive(PrimitiveType type) | GameObject | Creates a cube, sphere, capsule, etc. with mesh and collider. |
For Instantiate, Destroy and DontDestroyOnLoad, see Object.
Code Samples
Getting components safely
using UnityEngine;
public class Damager : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
// Does the thing we hit have a Health script?
if (other.TryGetComponent<Health>(out Health health))
{
health.TakeDamage(10);
}
}
}
Showing and hiding a menu
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
[SerializeField] private GameObject menuPanel; // drag the menu UI object here
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
bool show = !menuPanel.activeSelf;
menuPanel.SetActive(show);
Time.timeScale = show ? 0f : 1f; // freeze the game while the menu is open
}
}
}
Building an object entirely from code
GameObject go = new GameObject("Enemy"); // an empty GameObject with only a Transform
go.transform.position = new Vector3(0f, 1f, 5f);
go.AddComponent<Rigidbody>();
go.AddComponent<BoxCollider>();
go.tag = "Enemy"; // the tag must already exist in Tags & Layers
What happens at runtime
SetActive(false)
OnDisableis called on every script on the object and on all its children.- The object stops rendering, its colliders are removed from physics, and
Update/FixedUpdatestop being called. - Coroutines running on its scripts are stopped (and won't resume when reactivated).
- Children keep their own
activeSelfvalue, but theiractiveInHierarchybecomesfalse.
SetActive(true)
OnEnableis called on enabled scripts. If a script'sAwakehas never run (object started inactive), it runs now, followed later byStart.
Common Mistakes
GameObject.Findcan't find inactive objects. Keep a reference in a serialized field instead.- Calling Find or GetComponent every frame. Cache the result in
AwakeorStart. - Comparing tags with
==. Works, butCompareTagis faster and doesn't allocate memory. - Checking
activeSelfwhen you mean "is it visible". A child can beactiveSelf == truebut hidden because its parent is off. UseactiveInHierarchy.
See also
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.