Order of execution of Unity scripts

Source: Internet
Author: User

The functions commonly used in unity scripts are the following, and their order is executed in the direction of the arrows.
Awake->oneable-> Start, fixedupdate-> Update, lateupdate->ongui->ondisable->ondestroy

1.Awake

Used to initialize a variable or game state before the game starts. It is called only once throughout the lifetime of the script. Awake is called after all objects have been initialized, so you can safely talk to other objects or search for them with functions such as Gameobject.findwithtag (). the awake on each game object are called in random order . Therefore, you should use awake to set up a reference between scripts and use start to pass information awake is always called before start. It cannot be used to execute a synergistic program.

2.Start

Called only before the update function is called the first time. Start is called only once in the life cycle of the behaviour. The difference between it and awake is that start is only called when the script instance is enabled. You can adjust the deferred initialization code as needed. Awake is always executed before start. This allows you to coordinate the order of initialization. In all script instances, the start function is always called after the awake function.

3.FixedUpdate

Fixed frame update, in the Unity navigation menu bar, click "Edit"--"Project Setting" and "Time" menu item, the Inspector view on the right will pop up the timer manager, where "Fixed timestep" The option is used to set the update frequency for fixedupdate (), and the update frequency defaults to 0.02s.

4.Update

Normal frame updates to update the logic. Each frame is executed, when processing rigidbody, you need to use fixedupdate instead of update. For example: When you add a force to a rigid body, you must apply a fixed frame in the fixedupdate instead of the frame in the update. (both frame lengths differ) Fixedupdate, every fixed frame is executed once, and update is different fixedupdate is rendered frame execution, if your rendering efficiency is low, fixedupdate calls will be followed down. The Fixedupdate comparison is applicable to the calculation of the physics engine because it is related to each frame rendering. Update is more appropriate for control.

the difference between Update () and fixedupdate ()

Literally, they are called at the time of the update and will be looped. However, update will be called each time a new frame is rendered. And Fixedupdate will be called at every fixed time interval, so what if the time interval between update and fixedupdate is the same? The answer is not necessarily, because the update is affected by the current rendered object, more precisely the number of triangles, sometimes fast and sometimes slow, the frame rate changes, and the time interval at which the update is called changes. But Fixedupdate is not affected by the frame rate change, it is called at a fixed time interval, then how to set this time interval? Edit->project Setting->time below the fixed timestep.

5.LateUpdate

Called after all update function calls, like fixedupdate, each frame is called for execution, which can be used to adjust the order in which the scripts are executed. For example: When an object moves in the update, the camera following the object can be implemented in the lateupdate. Lateupdate, which is called at the end of each frame update execution, is not invoked until after all update finishes, and is more appropriate for the execution of the command script. Official online example is the camera follow, all the update operation is done before the camera, or there may be a camera has been pushed, but the perspective of the role of the empty frame appears.

the difference between update and lateupdate

In the Holy Scriptures, Lateupdate is interpreted as a sentence, and Lateupdate is called after all the update function calls. This can be used to adjust the script execution order. For example: When an object moves in the update, the camera following the object can be implemented in the lateupdate.

Lateupdate is executed later than all update. For example: There are 2 scripts in the game, script 1 contains update and lateupdate, and script 2 contains update, so when the game executes, each frame will execute after the update in 2 scripts executes lateupdate. Although executed in the same frame, the update executes first and lateupdate is executed late.

Now suppose that there are 2 different scripts that control an object in the update, and when one of the scripts changes the orientation, rotation, or other parameters of the object, and the other footstep is changing these things, the orientation and rotation of the object will appear a certain amount of repetition. If there is another object that follows the object in the update to move and rotate, the object that follows will appear jittery. If it is followed in lateupdate, it will only follow the last position and rotation of all the update executions, thus preventing jitter.

6.OnGUI

Called when a GUI event is rendered and processed. For example: You often use it when you draw a button or label. This means that the Ongui is also executed once per frame.

7.Reset

Called when the user taps the reset button in the View panel or when the component is first added. This function is called only in edit mode. Reset is most commonly used to give a default value in the Inspector.

8.OnDisable

Ondisable is called when an object is destroyed and can be used for arbitrary cleanup of code. When the script is unloaded, ondisable is called, and Onenable is called after the script is loaded. Note: ondisable cannot be used in a synergistic program.

9.OnDestroy

This function is called when the Monobehaviour will be destroyed. The OnDestroy will only be called on a game object that has been activated in advance. Note: OnDestroy also cannot be used in a synergistic program.

Order of execution of Unity scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.