The Lifecycle Order
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 73 of 85.
Unity calls a script's methods in a fixed order, from the moment its object appears to the moment it is destroyed:
Awake(): once, when the object is loaded or spawned.OnEnable(): right after, and again every time the object is shown.Start(): once, before the script's first frame.FixedUpdate(): on the physics clock, 50 times a second by default, so zero, one or several times in a frame, always before that frame'sUpdate().Update(): once a frame.LateUpdate(): once a frame, after everyUpdate().OnDisable(): every time the object is hidden, and before it is destroyed.OnDestroy(): once, when the object is destroyed.
Steps 4 to 6 repeat on every frame for as long as the object is active. A spawned object goes through the same list: Instantiate runs the copy's Awake() and OnEnable() before it returns, and the copy's Start() comes before its first Update().
Two habits follow from the order. Code in Awake() sets up an object's own fields in time for every other object's Start(). And physics code goes in FixedUpdate(), whose steps are always 0.02 seconds apart, however fast or slow the frames come.
Try it yourself
This lesson doesn't include a code challenge.
All lessons in Unity Scripting Basics
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling Star12The Script Lifecycle
Awake and StartUpdate and LateUpdateOnEnable and OnDisableThe Lifecycle OrderRecap: Lifecycle LoggerPractice on your own: Unity playground