MonoBehaviour
TypeClass
NamespaceUnityEngine
InheritsBehaviour > Component > Object
Used forEvery script you attach to a GameObject

MonoBehaviour

MonoBehaviour is the base class for your scripts. Inheriting from it (: MonoBehaviour) turns your class into a component that you can attach to GameObjects, and lets Unity call methods like Start and Update on it automatically.

Properties

Inherited from Behaviour and Component. You can use these directly in any script.

NameTypeDescription
enabledboolThe checkbox next to the script in the Inspector. When false, Update, FixedUpdate and LateUpdate stop being called.
gameObjectGameObjectThe GameObject this script is attached to.
transformTransformThat GameObject's Transform.
isActiveAndEnabledboolTrue if the script is enabled and its GameObject is active.
tag, namestringThe GameObject's tag and name.

Methods

MethodReturnsDescription
StartCoroutine(IEnumerator routine)CoroutineStarts a coroutine. See Coroutines.
StopCoroutine(Coroutine routine)voidStops a coroutine started by this script.
StopAllCoroutines()voidStops every coroutine started by this script.
Invoke(string methodName, float time)voidCalls a method on this script after time seconds.
InvokeRepeating(string methodName, float time, float repeatRate)voidCalls a method after time seconds, then every repeatRate seconds.
CancelInvoke()voidCancels all Invoke calls on this script. Pass a method name to cancel just one.
GetComponent<T>()TSame as gameObject.GetComponent<T>().

Messages (called by Unity)

A phone directory. Unity looks up your class and writes down which of these method names it has, like phone numbers. When a moment comes (a new frame, a collision), Unity calls those numbers. If you misspell one (update instead of Update), it's a wrong number: nothing breaks, nothing warns you, the phone just never rings.

These are like events. You don't call them; you write a method with the exact name and parameters and Unity calls it. They can be private.

MessageWhen it's called
Awake()Once, when the script instance is loaded.
OnEnable()Each time the script/object becomes enabled.
Start()Once, before the first Update.
Update()Every frame.
FixedUpdate()Every physics step.
LateUpdate()Every frame, after all Update calls.
OnDisable()When the script/object becomes disabled.
OnDestroy()When the object is destroyed.
OnCollisionEnter(Collision c)This object started touching another collider. Also Stay and Exit.
OnTriggerEnter(Collider other)Another collider entered this trigger (or vice versa). Also Stay and Exit.
OnMouseDown()The player clicked on this object's collider. (Old Input Manager only.)
OnBecameVisible() / OnBecameInvisible()Any camera started / stopped seeing this object's renderer. Includes the Scene view camera in the Editor.
OnApplicationPause(bool paused)The app lost/regained focus on mobile (good time to save).
OnApplicationQuit()Before the application quits.
OnValidate()Editor only: a value was changed in the Inspector. Good for clamping values.
Reset()Editor only: the component was first added or Reset was clicked. Good for default references.
OnDrawGizmos()Editor only: draw helper shapes in the Scene view.

For the exact order, see Script Lifecycle.

Code Samples

A complete, typical script

using UnityEngine;

public class Turret : MonoBehaviour
{
    [SerializeField] private Transform target;
    [SerializeField] private float range = 10f;
    [SerializeField] private float fireInterval = 1f;

    private float nextFireTime;

    void Update()
    {
        if (target == null) return;

        float distance = Vector3.Distance(transform.position, target.position);
        if (distance > range) return;

        transform.LookAt(target);

        if (Time.time >= nextFireTime)
        {
            Debug.Log("Fire!");
            nextFireTime = Time.time + fireInterval;
        }
    }

    // Draw the range in the Scene view so designers can see it.
    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range);
    }
}

Invoke: a simple delayed call

using UnityEngine;

public class Trap : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            Invoke(nameof(Snap), 0.5f); // nameof avoids typos in the string
        }
    }

    void Snap()
    {
        Debug.Log("Snap!");
    }
}

What happens at runtime

  1. When a scene loads, Unity creates an instance of your class for each GameObject that has it and fills in serialized fields from saved data.
  2. Unity looks for the message methods (Update, Start...) by name. If your class has them, it calls them at the right time. This is why the spelling must be exact.
  3. If the script has no Update method at all, Unity doesn't call anything each frame for it, which saves time.
  4. Setting enabled = false stops the per-frame messages, but collision/trigger messages and coroutines still run.

Common Mistakes

See also