# The life cycle

# Life cycle flow chart

Lifecycle

# Life cycle introduction

# Initialization

When scene resource is loaded, or prefab ) When an instance is added to the scene for the first time, all active nodes and components will execute the initialization life cycle in batches.

The activation state of nodes and components is not only affected by its own active property, but also by the inheritance of the parent node. If the parent node is in the inactive state, then the child node must be in the inactive state.

During the batch execution initialization life cycle, the following life cycle callbacks will be executed:

-onAwake The component is executed when it is activated on the field for the first time, and the life cycle will only be executed once. If the component is inactive when the scene or prefab is played for the first time, then onAwake will be executed after the component is activated.

-onEnable If the component is still active after onAwake, then the life cycle will be executed.

-onStart If the component is still active after onEnable, then the life cycle will be executed. (This life cycle will only be executed once)

The life cycle of each component in the batch initialization will be uniformly managed. The onAwake of any component must be before the onEnable of any component, and in the same way, the onEnable must be before onStart.

It is worth noting that if the component properties refer to another component, then the reference information has been established in the onAwake stage, and users can access it with confidence.

For non-batch initialization scenarios (the user adds a new component to an active node in the script), the component only guarantees that its life cycle sequence conforms to the above sequence.

# renew

The initialized component is active and will be called by the scheduler at the specified position of the game loop to update the life cycle function. The main life cycle includes the following methods:

-onUpdate

The most widely used life cycle in game logic is the onUpdate life cycle, which can cover most of the business scenario requirements and interactions: changing object positions, adjusting camera parameters, setting animation states, etc. The frequency of execution of onUpdate is once per frame.

The user's script components run before the game's built-in components (animation, physics, rendering components, etc.)

-onFixedUpdate

The execution frequency of onFixedUpdate is determined by the configuration parameter engine.gane.physicsSystem.maxPhysicsDeltaTime, its default value is 0.1, and it will be executed stably at a stable frequency. So usually the execution frequency of onFixedUpdate is higher than the frequency of game frame refresh (it may be executed multiple times in some frames). All physical calculations and updates occur after onFixedUpdate.

-onLateUpdate onLateUpdate is the same as onUpdate, it is executed once per frame, the difference is that its execution timing is after onUpdate, animation, physics, so it is applicable to adjust the camera aimed at the character when using the scene, similar to this Depends on the logic of physics and animation.

The execution timing of the update is strictly in accordance with the timing in the chart. If a new component is added before the next frame, during or after the frame update (for example, during the onUpdate process, during the callback of the network request), then Although the initialization life cycle of this component will be executed immediately, all the update life cycles will be executed in the next frame.

# Activation, deactivation and destruction

Component activation and deactivation can be achieved by setting the active property. When the component's activation state (affected by the parent node) is switched, the onEnable / onDisable method will be triggered immediately. When the component is destroyed (removeComponent or through the node's destroy method), if it is activated, then the onDisable of the component will be executed immediately, and then the onDestroy life cycle will be executed; on the contrary, if If it is in an inactive state, the onDestroy life cycle will be executed immediately.

In the entire life cycle of a component, onEnable and onDisable must be executed the same number of times, and onAwake and onDestroy must also be executed the same number of times. This means that if the user destroys a component that has never been activated, the onDestroy life cycle will not be triggered when the component is destroyed.

# priority

In some cases, users hope that component updates can be performed in a certain order. In this case, the user can adjust the priority by setting the priority property of the component. The component with the higher priority will be scheduled first. Priority does not support dynamic adjustment, please set it in the editor as much as possible.

Note: There is no guarantee for the scheduling order of components of the same priority.