As we all know, the order in which a script in unity is executed is awake, Start, Update, lateupdate, etc., but what is the order of execution between different scripts?
For example, I have two scripts Script1 and Script2, so what is the order of awake execution for these two scripts?
To know the truth we have to understand the unity behind the operating principle, in fact, unity backstage is a single-threaded execution, so-called coprocessor are pseudo-multithreading. The awake of different scripts in the background is the truth:
Background awake () {script 0Awake (); script 1Awake (); ...}
Start, update, and other functions are the same, then the question is, so many scripts, what is the order of their execution?
This relationship I really don't understand the rules, but unity provides a function to set the order of execution between different scripts:
1. First select a script file in the Project window and then display the following in the Inspector Card:
2, click on the upper right corner execution order display as follows:
3. Click the plus sign to add the script file you want to sort.
4. Drag the script file to sort.
This will initialize the script in the order in which you ordered it.
The reason we want to set the order of execution between scripts is primarily to avoid the following scenario
public class Script1:monobehaviour {void Awake () {gameobject.createprimitive (Primitivetype.cube);}}
public class Script2:monobehaviour {void Awake () {Gameobject go = Gameobject.find ("Cube");D ebug. Log (go.name);} }
this will cause an error if SCRIPT2 is executed before Scirpt1. Therefore, the script initialization order needs to be set.
But there is another way to solve these problems:
That is to create a cube in SCRIPT1 's awake, to access the cube at the start of Script2, so there is no error.
Because the background is also strictly in the order of awake earlier than start, that is, the awake of all scripts executed, and then execute all the script start.
[UNITY-20] Unity order of execution between different scripts