In unity, you can create many scripts at the same time, and you can bind to different game objects individually, each of which runs in its own life cycle. The script is about compiling and executing, and this article is going to look at the compilation and execution order of the scripts in unity.
Let's talk about the order of execution first.
The script execution sequence given by the official is as follows:
We can do a little experiment to test it:
Create three game objects in the hierarchy view, create three scripts in Project view, as shown in, and then bind the script to the corresponding game object in order:
The code for the three script is exactly the same, just a little bit of a name distinction:
1 usingSystem.Collections;2 3 Public classScring0:monobehaviour4 {5 voidAwake ()6 {7Debug.Log ("Script0 ======= Awake");8 }9 Ten BOOLIsupdate =false; One voidUpdate () A { - if(!isupdate) - { theDebug.Log ("Script0 ======= Update"); -Isupdate =true; - } - } + - BOOLIslateupdate =false; + voidlateupdate () A { at if(!islateupdate) - { -Debug.Log ("Script0 ======= lateupdate"); -Islateupdate =true; - } - } in}
Play the game and see the order in which they are executed. As shown, awake, Update, lateupdate, regardless of the number of times the game is run, the order in which they are executed is exactly the same.
Then we do a test, the Script0 Update method commented out!!
1 usingSystem.Collections;2 3 Public classScring0:monobehaviour4 {5 voidAwake ()6 {7Debug.Log ("Script0 ======= Awake");8 }9 Ten //bool isupdate = false; One //void Update () A // { - //if (!isupdate) - // { the //Debug.Log ("Script0 ======= Update"); - //isupdate = true; - // } - // } + - BOOLIslateupdate =false; + voidlateupdate () A { at if(!islateupdate) - { -Debug.Log ("Script0 ======= lateupdate"); -Islateupdate =true; - } - } in}
Run the game again to see its results. The script executes in exactly the same order as before, Script0 even if the update method is removed, but it does not execute the Lateupdate method directly, but waits for the Update method in both SCRIPT1 and Script2 to finish executing. Then execute all the lateupdate methods.
With these two examples, we can clearly determine how the unity daemon executes the script. Each script awake, Start, Update, Lateupdate, Fixedupdate, and so on, all of the methods are rolled up in the background:
1 awake in the background () 2 {3 Awake () in script 0; 4 Awake () in script 1; 5 Awake () in script 2; 6 }
Backstage methods Awake, update, lateupdate and so on, are in accordance with the order, and so on all the game objects on the script awake execution, then go to start, update, Lateupdate and other methods.
1 Update in the background () 2 {3 Update () in script 0; 4 Update () in script 1; 5 Update () in script 2; 6 }
Then let's look at a scenario where you create a cube object in the Awake method of script 0, and then get the cube object in the Awake method of script 2. The code is as follows:
1 code for script 0:2 usingUnityengine;3 usingSystem.Collections;4 5 Public classScript2:monobehaviour6 {7 voidAwake ()8 {9 gameobject.createprimitive (primitivetype.cube);Ten } One } A - code for Script 2: - usingUnityengine; the usingSystem.Collections; - - Public classScript0:monobehaviour - { + voidAwake () - { +Gameobject go = Gameobject.find ("Cube"); A Debug.Log (go.name); at } -}
If the execution order of the script is to execute SCRIPT0 first and then execute SCRIPT2, then awake in Script2 can get to the cube object correctly, but if the script executes Script2 first, then Script0, Then the SCRIPT2 will certainly report a null pointer error.
So there are a lot of scripts in the actual project, and the order of their execution is not known to us (and is executed according to the stack structure, that is, the script that is bound to the game object is executed first.) This is worth studying). However, in general, it is recommended to create a game object or Resources.load (Prefab) object in the Awake method, and then get the game object or component in the Start method so that it is foolproof.
In addition, Unity provides a way to set the order in which scripts are executed, which you can see in the Inspector panel in the Script Execution Order menu item, edit-Settings:
Clicking on the "+" in the lower right corner will bring up the drop-down window, including all the scripts in the game. After the script is added, you can use the mouse to drag the script to sort the script, the smaller the number behind the name of the foot, the more the script, the more the first execution. The default time in it indicates the order in which scripts are executed without setting the execution order of the scripts.
Execution order and compilation order of scripts in Unity3d