GameObject
TypeClass (sealed)
NamespaceUnityEngine
InheritsObject
In scriptsgameObject (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.

NameTypeDescription
namestringName shown in the Hierarchy.
transformTransformIts position, rotation and scale. Every GameObject has one.
activeSelfboolIs this object's own checkbox ticked? (Read only. Use SetActive to change.)
activeInHierarchyboolIs it actually active? False if it or any parent is inactive.
tagstringA label like "Player" or "Enemy". Add tags in the Tags & Layers settings.
layerintLayer number (0-31). Used for physics filtering and camera culling.
sceneSceneThe scene it belongs to.

Methods

MethodReturnsDescription
SetActive(bool value)voidTurns the object (and all its children) on or off. Off means no rendering, no physics, no Update.
GetComponent<T>()TGets a component of type T on this object, or null if there isn't one.
TryGetComponent<T>(out T component)boolLike GetComponent but returns true/false. Cleaner when the component might be missing.
GetComponentInChildren<T>()TSearches this object, then its children (depth-first).
GetComponentInParent<T>()TSearches this object, then its parents.
GetComponents<T>()T[]All components of type T on this object.
AddComponent<T>()TAdds a new component and returns it. Its Awake runs immediately.
CompareTag(string tag)boolChecks the tag. Faster than tag == "Player".

Static Methods

MethodReturnsDescription
GameObject.Find(string name)GameObjectFinds an active object by name. Slow; avoid in Update.
GameObject.FindWithTag(string tag)GameObjectFinds one active object with the tag.
GameObject.FindGameObjectsWithTag(string tag)GameObject[]Finds all active objects with the tag.
GameObject.CreatePrimitive(PrimitiveType type)GameObjectCreates 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)

  1. OnDisable is called on every script on the object and on all its children.
  2. The object stops rendering, its colliders are removed from physics, and Update / FixedUpdate stop being called.
  3. Coroutines running on its scripts are stopped (and won't resume when reactivated).
  4. Children keep their own activeSelf value, but their activeInHierarchy becomes false.

SetActive(true)

  1. OnEnable is called on enabled scripts. If a script's Awake has never run (object started inactive), it runs now, followed later by Start.

Common Mistakes

See also