Legacy Documentation: Version 2017.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MonoBehaviour

class in UnityEngine

/

Inherits from:Behaviour

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

MonoBehaviour is the base class from which every Unity script derives.

When you use C#, you must explicitly derive from MonoBehaviour. When you use UnityScript (a type of JavaScript), you do not have to explicitly derive from MonoBehaviour.

Note: There is a checkbox for disabling MonoBehaviour on the Unity Editor. It disables functions when unticked. If none of these functions are present in the script, the Editor does not display the checkbox. The functions are:

Awake()
Start()
Update()
FixedUpdate()
LateUpdate()
OnGUI()
OnDisable()
OnEnable()


See Also: The Deactivating GameObjects page in the manual.

Variables

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.

Public Functions

CancelInvokeCancels all Invoke calls on this MonoBehaviour.
InvokeInvokes the method methodName in time seconds.
InvokeRepeatingInvokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
IsInvokingIs any invoke on methodName pending?
StartCoroutineStarts a coroutine.
StopAllCoroutinesStops all coroutines running on this behaviour.
StopCoroutineStops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.

Static Functions

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

Messages

AwakeAwake is called when the script instance is being loaded.
FixedUpdateThis function is called every fixed framerate frame, if the MonoBehaviour is enabled.
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 application pauses.
OnApplicationQuitSent to all game objects before the application is quit.
OnAudioFilterReadIf OnAudioFilterRead is implemented, Unity will insert a custom filter into the audio DSP chain.
OnBecameInvisibleOnBecameInvisible is called when the renderer is no longer visible by any camera.
OnBecameVisibleOnBecameVisible is called when the renderer became visible by any camera.
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/rigidbody that is touching rigidbody/collider.
OnCollisionStay2DSent each frame where a collider on another object is touching this object's collider (2D physics only).
OnConnectedToServerCalled on the client when you have successfully connected to a server.
OnControllerColliderHitOnControllerColliderHit is called when the controller hits a collider while performing a Move.
OnDestroyThis function is called when the MonoBehaviour will be destroyed.
OnDisableThis function is called when the behaviour becomes disabled () or inactive.
OnDisconnectedFromServerCalled on the client when the connection was lost or you disconnected from the server.
OnDrawGizmosImplement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.
OnDrawGizmosSelectedImplement OnDrawGizmosSelected to draw a gizmo if the object is selected.
OnEnableThis function is called when the object becomes enabled and active.
OnFailedToConnectCalled on the client when a connection attempt fails for some reason.
OnFailedToConnectToMasterServerCalled on clients or servers when there is a problem connecting to the MasterServer.
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.
OnMasterServerEventCalled on clients or servers when reporting events from the MasterServer.
OnMouseDownOnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
OnMouseDragOnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseEnterCalled when the mouse enters the GUIElement or Collider.
OnMouseExitCalled when the mouse is not any longer over the GUIElement or Collider.
OnMouseOverCalled every frame while the mouse is over the GUIElement or Collider.
OnMouseUpOnMouseUp is called when the user has released the mouse button.
OnMouseUpAsButtonOnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.
OnNetworkInstantiateCalled on objects which have been network instantiated with Network.Instantiate.
OnParticleCollisionOnParticleCollision is called when a particle hits a Collider.
OnParticleTriggerOnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.
OnPlayerConnectedCalled on the server whenever a new player has successfully connected.
OnPlayerDisconnectedCalled on the server whenever a player disconnected from the server.
OnPostRenderOnPostRender is called after a camera finished rendering the scene.
OnPreCullOnPreCull is called before a camera culls the scene.
OnPreRenderOnPreRender is called before a camera starts rendering the scene.
OnRenderImageOnRenderImage is called after all rendering is complete to render image.
OnRenderObjectOnRenderObject is called after camera has rendered the scene.
OnSerializeNetworkViewUsed to customize synchronization of variables in a script watched by a network view.
OnServerInitializedCalled on the server whenever a Network.InitializeServer was invoked and has completed.
OnTransformChildrenChangedThis function is called when the list of children of the transform of the GameObject has changed.
OnTransformParentChangedThis function is called when the parent property of the transform of the GameObject has changed.
OnTriggerEnterOnTriggerEnter is called when the Collider other enters the trigger.
OnTriggerEnter2DSent when another object enters a trigger collider attached to this object (2D physics only).
OnTriggerExitOnTriggerExit is called when the Collider other has stopped touching the trigger.
OnTriggerExit2DSent when another object leaves a trigger collider attached to this object (2D physics only).
OnTriggerStayOnTriggerStay is called once per frame for every Collider other that is touching the trigger.
OnTriggerStay2DSent each frame where another object is within a trigger collider attached to this object (2D physics only).
OnValidateThis function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
OnWillRenderObjectOnWillRenderObject is called for each camera if the object is visible.
ResetReset to default values.
StartStart is called on the frame when a script is enabled just before any of the Update methods is called the first time.
UpdateUpdate is called every frame, if the MonoBehaviour is enabled.

Inherited members

Variables

enabledEnabled Behaviours are Updated, disabled Behaviours are not.
isActiveAndEnabledHas the Behaviour had enabled called.
gameObjectThe game object this component is attached to. A component is always attached to a game object.
tagThe tag of this game object.
transformThe Transform attached to this GameObject.
hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
nameThe name of the object.

Public Functions

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagIs this game object tagged with tag ?
GetComponentReturns the component of Type type if the game object has one attached, null if it doesn't.
GetComponentInChildrenReturns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentInParentReturns the component of Type type in the GameObject or any of its parents.
GetComponentsReturns all components of Type type in the GameObject.
GetComponentsInChildrenReturns all components of Type type in the GameObject or any of its children.
GetComponentsInParentReturns all components of Type type in the GameObject or any of its parents.
SendMessageCalls the method named methodName on every MonoBehaviour in this game object.
SendMessageUpwardsCalls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.
GetInstanceIDReturns the instance id of the object.
ToStringReturns the name of the game object.

Static Functions

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadMakes the object target not be destroyed automatically when loading a new scene.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.
InstantiateClones the object original and returns the clone.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.

Did you find this page useful? Please give it a rating: