The difference and use of Awake and Start in Untiy

The difference and use of Awake and Start in Untiy

I encountered this problem again recently, and hereby make a memory and record. Remember to search for things on the Internet, you must try to verify it yourself. Many articles are reprinted indiscriminately, and they are published without serious verification at all. I have tested and searched for information below, and finally there is an attached test project, which can be tested by yourself.

Unity Codex explained

Let's take a look at the explanation in the Unity Canon.

Awake()
Awake is called when a script instance is loaded. It can also be understood that when the object is activated, when the object is activated, it will automatically load its own script,
regardless of whether the script is checked or not.
That is to say, only the activated object will execute the Awake of the script. Note that this is the active object, not the script.

It is understood that it is called when the object is activated.

Awake is used to initialize variables or game state before the game starts. It is called only once during 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 like GameObject.FindWithTag . Awke on each game object is called in random order. Therefore, you should use Awake to set up references between scripts and Start to pass information. Awake is always called before Start. It cannot be used to execute coroutines.

Start()

Start is called only before the Update function is called for the first time. Start is called only once in the behavior's life cycle. It differs from Awake in that Start is only called when the script instance is enabled.

It is understood that it is called when the script is checked.

You can adjust the lazy initialization code as needed. Awake is always executed before Start. This allows you to coordinate initialization order.

Law summary

condition Awake Start
Object not activated X X
Object activated, script not activated V X
The object is activated, the script is also activated V V

Please remember that these two functions will only be executed once, no matter if you repeatedly check or cancel the disabled state of the script

Awake execution order for multiple objects

The Awake scripts of multiple object scripts are executed in random order, and Start will also maintain this order. If you need to manually change the execution order of the scripts, you
need to set it yourself.
Overall, the execution order of Awake and Start must be maintained.
Unity script initialization sequence and adjustment methodhttps:
//jingyan.baidu.com/article/0a52e3f4d47152bf62ed7297.html

Other considerations

In addition, there is another important point for both the Awake function and the Start function, that is, when there is a parent-child relationship between game objects (no matter how many levels). At this point, the state of the parent game object (on or off) completely determines the execution of script functions on the child game object. In general, only when the parent game object is turned on, the program will consider whether to call script functions (Awake and Start, etc.) on the child game object. From top to bottom, and so on.

Please note that if there is an abnormal situation in Awake or Start, an error will be reported directly and the operation will be stopped.

Experimental Engineering Test

Link: https://pan.baidu.com/s/1Nk-IG5Ahkx55YBytw-Ikug Password: y5aq

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340068&siteId=291194637