Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


MonoBehaviour

MonoBehaviour is a base class that many Unity scripts derive from.
Read time 11 minutesLast updated 2 days ago

Definition

public class MonoBehaviour : Behaviour

Remarks

MonoBehaviour offers life cycle functions that make it easier to develop with Unity.
MonoBehaviours always exist as a Component of a GameObject, and can be instantiated with GameObject.AddComponent.
A MonoBehaviour can be deleted with Object.Destroy or Object.DestroyImmediate. When the parent GameObject is destroyed all components are automatically deleted, including MonoBehaviours.
After the underlying component is destroyed, the C# object for the MonoBehaviour remains in memory until garbage is collected. A MonoBehaviour in this state acts as if it is null. For example, it returns true for a "obj == null" check. However, this class doesn't support the null-conditional operator (?.) and the null-coalescing operator(??).
When a MonoBehaviour is serialized, the value of C# fields are included according to Unity's Serialization rules. See Script Serialization for details. The serialized data also includes internal properties, such as the reference to the MonoScript that tracks the implementation class for the object.
For code samples, see the individual MonoBehaviour methods.
Note: In the Editor, when the MonoBehaviour component is deactivated through the checkbox in the Inspector, it stops receiving calls to its Event functions.
Additional Resources: The Deactivating GameObjects page in the manual.

Properties

Property

Description

destroyCancellationTokenCancellation token raised when the MonoBehaviour is destroyed (Read Only).
didAwakeReturns a boolean value which represents if Awake was called.
didStartReturns a boolean value which represents if Start was called.
runInEditModeAllow a specific instance of a MonoBehaviour to run in edit mode (only available in the editor).
useGUILayoutDisabling this lets you skip the GUI layout phase.

Methods

Method

Description

AwakeUnity calls Awake when loading an instance of a script component.
CancelInvokeCancels all Invoke calls on this MonoBehaviour.
FixedUpdateUpdate called at regular, fixed intervals as part of Unity's physics update loop.
InvokeInvokes the method methodName in time seconds.
InvokeRepeatingInvokes the specified method after a specified delay, then repeatedly at the specified rate.
IsInvokingIs any invoke pending on this MonoBehaviour?
LateUpdateLateUpdate is called every frame, if the Behaviour is enabled.
OnAnimatorIKCallback for setting up animation IK (inverse kinematics).
OnAnimatorMoveCallback for processing animation movements for modifying root motion.
OnApplicationFocusSent to all GameObjects when the player gets or loses focus.
OnApplicationPauseSent to all GameObjects when the playing application pauses or resumes on losing or regaining focus.
OnApplicationQuitSent to all active GameObjects before the application quits.
OnAudioFilterReadIf OnAudioFilterRead is implemented, Unity will insert a custom filter into the audio DSP chain.
OnBecameInvisibleCalled when the renderer is no longer visible to any camera.
OnBecameVisibleCalled when the renderer becomes visible to any camera.
OnChildRectTransformDimensionsChangeCalled when a child RectTransform's dimensions change.
OnCollisionEnterOnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionEnter2DSent when an incoming collider makes contact with this object's collider (2D physics only).
OnCollisionExitOnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionExit2DSent when a collider on another object stops touching this object's collider (2D physics only).
OnCollisionStayOnCollisionStay is called once per frame for every Collider or Rigidbody that touches another Collider or Rigidbody.
OnCollisionStay2DSent each frame where a collider on another object is touching this object's collider (2D physics only).
OnControllerColliderHitOnControllerColliderHit is called when the CharacterController component hits a collider while performing a Move.
OnDestroyCalled when a GameObject or component is about to be destroyed.
OnDisableCalled when a component itself is disabled or its parent GameObject is deactivated.
OnDrawGizmosImplement OnDrawGizmos if you want to draw gizmos that can be pickable and are always drawn.
OnDrawGizmosSelectedImplement OnDrawGizmosSelected to draw a gizmo if the object is selected.
OnEnableCalled when a component of an active GameObject is first enabled.
OnGUIOnGUI is called for rendering and handling GUI events.
OnJointBreakCalled when a joint attached to the same game object broke.
OnJointBreak2DCalled when a Joint2D attached to the same game object breaks.
OnMouseDownOnMouseDown is called when the user presses the left mouse button while over the Collider.
OnMouseDragOnMouseDrag is called when the user has clicked on a Collider and is still holding down the mouse.
OnMouseEnterCalled when the mouse enters the Collider.
OnMouseExitCalled when the mouse is not any longer over the Collider.
OnMouseOverCalled every frame while the mouse is over the Collider.
OnMouseUpCalled on releasing the mouse button while the mouse pointer is over the Collider attached to this GameObject.
OnMouseUpAsButtonOnMouseUpAsButton is only called when the mouse is released over the same Collider as it was pressed.
OnParticleCollisionOnParticleCollision is called when a particle hits a Collider.
OnParticleSystemStoppedOnParticleSystemStopped is called when all particles in the system have died, and no new particles will be born. New particles cease to be created either after Stop is called, or when the duration property of a non-looping system has been exceeded.
OnParticleTriggerOnParticleTrigger is called when any particles in a Particle System meet the conditions in the trigger module.
OnParticleUpdateJobScheduledOnParticleUpdateJobScheduled is called when a Particle System's built-in update job has been scheduled.
OnPostRenderEvent function that Unity calls after a Camera renders the scene.
OnPreCullEvent function that Unity calls before a Camera culls the scene.
OnPreRenderEvent function that Unity calls before a Camera renders the scene.
OnRenderImageEvent function that Unity calls after a Camera has finished rendering, that allows you to modify the Camera's final image.
OnRenderObjectOnRenderObject is called after camera has rendered the Scene.
OnTransformChildrenChangedCalled when the list of direct children of a GameObject's Transform changes.
OnTransformParentChangedCalled when a direct or indirect parent of a GameObject's Transform changes.
OnTriggerEnterCalled when a collider enters a trigger collider.
OnTriggerEnter2DSent when another object enters a trigger collider attached to this object (2D physics only).
OnTriggerExitOnTriggerExit is called when a collider stops touching a trigger.
OnTriggerExit2DSent when another object leaves a trigger collider attached to this object (2D physics only).
OnTriggerStayCalled once per physics update for every collider that is touching the trigger.
OnTriggerStay2DSent once per physics update when another object is within a trigger collider attached to this object (2D physics only).
OnValidateEditor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
OnWillRenderObjectCalled for each camera if the object is visible and not a UI element.
ResetReset a component to default values.
StartStart is called on the frame when a script is enabled just before any of the Update methods are called the first time.
StartCoroutineStarts a coroutine.
StopAllCoroutinesStops all coroutines running on this MonoBehaviour.
StopCoroutineStops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.
UpdateUpdate is called every frame, if the MonoBehaviour is enabled.

Static Methods

Method

Description

printLogs message to the Unity Console (identical to Debug.Log).

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.