[UNITY-20] Unity order of execution between different scripts

Source: Internet
Author: User

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

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.