| Type | Class |
|---|---|
| Namespace | UnityEngine |
| Inherits | Behaviour > Component > Object |
| Used for | Every 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.
| Name | Type | Description |
|---|---|---|
enabled | bool | The checkbox next to the script in the Inspector. When false, Update, FixedUpdate and LateUpdate stop being called. |
gameObject | GameObject | The GameObject this script is attached to. |
transform | Transform | That GameObject's Transform. |
isActiveAndEnabled | bool | True if the script is enabled and its GameObject is active. |
tag, name | string | The GameObject's tag and name. |
Methods
| Method | Returns | Description |
|---|---|---|
StartCoroutine(IEnumerator routine) | Coroutine | Starts a coroutine. See Coroutines. |
StopCoroutine(Coroutine routine) | void | Stops a coroutine started by this script. |
StopAllCoroutines() | void | Stops every coroutine started by this script. |
Invoke(string methodName, float time) | void | Calls a method on this script after time seconds. |
InvokeRepeating(string methodName, float time, float repeatRate) | void | Calls a method after time seconds, then every repeatRate seconds. |
CancelInvoke() | void | Cancels all Invoke calls on this script. Pass a method name to cancel just one. |
GetComponent<T>() | T | Same 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.
| Message | When 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
- 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.
- 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. - If the script has no
Updatemethod at all, Unity doesn't call anything each frame for it, which saves time. - Setting
enabled = falsestops the per-frame messages, but collision/trigger messages and coroutines still run.
Common Mistakes
- Using
newon a MonoBehaviour.new Turret()gives a broken object and a warning. UseAddComponent<Turret>()orInstantiatea prefab. - Wrong spelling or parameters.
void OnTriggerEnter(Collision c)orvoid start()are silently never called. - Using Invoke with a method name string that doesn't exist. Nothing happens. Use
nameof(Method)so the compiler checks it. - File name ≠ class name. Unity can't add the script.
See also
- Your First Script
- Script Lifecycle
- ScriptableObject (for data that isn't attached to a GameObject)
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.