Lifecycles
AirshipBehaviours share many of the Unity MonoBehaviour lifecycle methods.
Lifecycle methods are automatically called, similar to an event handler, but without the need to explicitly connect them. All lifecycle methods are optional.
For a flowchart of execution order, refer to the Unity Docs.
Initialization
Awake
Called when the component first starts up. This is similar to its constructor. It will only run one time. It will not run until the GameObject is active and the component is enabled.
class Example extends AirshipBehaviour {
Awake() {
// Code
}
}OnEnable
Called when the component is enabled. When the component first runs, this will run right after Awake and before Start. This method will also run anytime the component is re-enabled after being disabled.
class Example extends AirshipBehaviour {
OnEnable() {
// Code
}
}Start
Called after Awake and after OnEnabled. Only called once. Generally, most startup code should go here.
Decommission
OnDisable
Called when the component is disabled. OnDisable is also called right before OnDestroy.
OnDestroy
Called when the component is removed or the GameObject is destroyed.
Update Lifecycles
Update
Called every frame while the component is active. deltaTime is the time (in seconds) since the previous update.
LateUpdate
Called every frame while the component is active. Equivalent behavior to Update, except all LateUpdate methods are called after all Update methods have run.
FixedUpdate
Called every physics step. FixedUpdate should be used when doing physics calculations.
Collisions
OnCollisionEnter
Called when a collider or rigidbody attached to the same GameObject as this component starts touching another collider or rigidbody.
OnCollisionStay
Called once per frame while a collision is occurring.
OnCollisionExit
Called when a collider or rigidbody attached to the same GameObject as this component stops touching another collider or rigidbody.
2D
For 2D collisions, there is also OnCollisionEnter2D, OnCollisionStay2D, and OnCollisionExit2D. Their methods look identical to the above examples, except with "2D" appended to the name and collision type.
Triggers
Please refer to the Unity docs for clarification between normal colliders and trigger colliders. Note that the argument provided is still a Collision object.
OnTriggerEnter
Called when a collider or rigidbody attached to the same GameObject as this component starts touching another collider or rigidbody.
OnTriggerStay
Called once per frame while a collision is occurring.
OnTriggerExit
Called when a collider or rigidbody attached to the same GameObject as this component stops touching another collider or rigidbody.
2D
For 2D collisions, there is also OnTriggerEnter2D, OnTriggerStay2D, and OnTriggerExit2D. Their methods look identical to the above examples, except with "2D" appended to the name and collision type.
Last updated