Summary of invocation of awake and start functions in unity (RPM)

Source: Internet
Author: User

When scripting in unity, there are a series of overridable (override) functions for us to use, with the awake and start two functions used as initialization and setup, almost in every script. Therefore, to correctly grasp the timing of these two functions, we will be able to avoid some errors in the program development process, improve the development efficiency. More lazy, so there is no, welcome everyone's criticism correct:)

1. Awake function

First, let's look at some of the explanations for the awake function in Unity's Reference manual:

The awake function is called when the script instance is loaded, and the awake function is not called until all the game objects have been initialized, and the awake function executes only once for the entire life cycle of the script instance. So, when the script instance is loaded, the entire life cycle of the script instance represents something, because there are doubts about these concepts, so on top of this, I have carried out some tests in the engine, through the printout of the way to see the implementation of the awake function, summarized as follows (no special instructions, Without switching the scene):

(1) If the initial state of the game object (that is, gameobject) is off, the awake function is not executed, and if the initial state of the game object is turned on, the awake function executes. Also, it is important to note that the execution of the awake function is not related to the state of the script instance (enabled or disabled), but rather to the state of the game object to which the script instance is bound.

(2) Without switching the scene, the original game object is in the closed state, when it is first opened, the awake function will be executed, and it will only be executed on the first turn, and the game object (Awake function has already been executed once) is turned on, it is turned off again, The awake function does not execute again. It appears that this corresponds to the case in the manual that is executed only once throughout the life cycle of the script instance.

(3) If the scene is reloaded, the execution of the awake function in the scene re-follows the two points above.

2. Start function

Let's look at some of the instructions in the Reference manual for the start function:

The start function is called before the update function is first called, and the start function executes only when the script instance is enabled; the start function is always executed after the awake function. On this basis, your own tests are summarized as follows (no special instructions are provided without switching scenarios):

(1) If the game object is closed, then the start function will not execute. Think of it as a matter of course, the game object is closed, the script instance will not be loaded, the awake function will not execute, the start function will not execute, and the update function will not execute.

(2) If the game object is turned on, the script instance bound on the object is disabled, then the start function will not execute. This is the feature of the start function, which is only executed when the script instance is enabled, as mentioned in this note. Note that this is different from the awake function.

(3) If the game object is turned on and the script instance bound by the object is turned on, the start function executes. And the start function will only execute when the script instance is first opened. If a script instance that has already been turned on is turned off and turned on again, the start function will not be executed again.

(4) If the scene is reloaded, then the execution of the start function in the scene re-conforms to the above three points.

In addition to this, there are also important to the awake function and start function, that is, when there is a parent-child relationship between the game objects (regardless of the level of how much). At this point, the state (on or off) of the parent game object completely determines the execution of the script function on the child game object. In general, it is only when the parent game object is turned on that the program will consider whether to invoke script functions (awake and start, etc.) on the child game object. Top-down, etc.

Finally, my own use of the awake and start functions is generally to get information about the game object or the script instance in the Awake function, and then make some initialization settings after getting it in the start function. Specific circumstances, but also according to the needs of flexible changes. The summary is here, thank you for reading, welcome to criticize.

Http://blog.csdn.net/haohan_meng

Summary of invocation of awake and start functions in unity (RPM)

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.