Unity Script Execution Order

Source: Internet
Author: User

Awake->oneable-> Start->-> fixedupdate-> Update, lateupdate->ongui->reset- >ondestroy

usingUnityengine; usingSystem.Collections;  Public classTimetest:monobehaviour {voidlateupdate () {print ("lateupdate"); }        voidOngui () {print ("Ongui"); }        voidAwake () {print ("Awake"); }        voidonenable () {print ("onenable"); }        voidStart () {print ("Start"); }       voidUpdate () {print ("Update"); }      voidfixedupdate () {print ("fixedupdate"); }           voidReset () {print ("onreset"); }             voidOnDestroy () {print ("OnDestroy"); }        voidondisable () {print ("ondisable"); }  }  

Printing results are as follows

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 for 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 " The Fixed timestep option sets the update frequency for fixedupdate () and the update frequency defaults to 0.02s.
4.Update: Normal frame update for logic update. 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.
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.

6.OnGUI: Called when rendering and handling GUI events. 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 will be called when the 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 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.

Note:

The cooperative program, that is, while the main program is running, open another logic processing to coordinate the execution of the current program. In other words, opening a co-worker is a thread that opens. In Unity3d, you can use the Monobehaviour.startcoroutine method to open a synergistic program, which means that the method must be called in a monobehaviour or class that inherits from Monobehaviour.

Unity Script Execution Order

Related Article

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.