On unity script life cycle and execution sequence

Time:2021-12-6
catalogue
  • 1、 Script lifecycle
  • 2、 Monobehavior life cycle diagram
  • 3、 Script execution order
  • 4、 Custom execution order

1、 Script lifecycle

Common inevitable events in unity script are shown in the following table

name Trigger timing purpose
Awake Called when a script instance is created For the initialization of game objects, note that the execution of wake is earlier than the start function of all scripts
OnEnable Called when the object becomes available or active purpose
Start The Update function is called before it runs for the first time. Initialization for game objects
Update Called once per frame Used to update game scenes and status
FixedUpdate Called once per fixed physical time interval Update for physical status
LateUpdate Each frame is called once (after update). It is used to update game scenes and states. Camera related updates are generally placed here
OnGUI Rendering and handling ongui events purpose
OnDisable Called when the current object is unavailable or inactive purpose
OnDestroy Called when the current object is destroyed purpose

Next, we will look at the call timing of these inevitable events with code

Create a new c# script, add the following code, and then hang it on any GameObject


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestTest : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log("Awake");
    }

    private void OnEnable()
    {
        Debug.Log("OnEnable");
    }

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Update");
    }

    private void FixedUpdate()
    {
        Debug.Log("FixedUpdate");
    }

    private void LateUpdate()
    {
        Debug.Log("LateUpdate");
    }

    private void OnGUI()
    {
        Debug.Log("OnGUI");
    }

    private void OnDisable()
    {
        Debug.Log("OnDisable");
    }

    private void OnDestroy()
    {
        Debug.Log("OnDestroy");
    }
}

The printing results are shown as follows:

It can be found that the wake and start functions are called once when the game object is created.
When the visibility state of the script is adjusted during the game, the OnEnable and OnDisable functions are called separately, while Awake and Start will not be called again. That is to say, once the script is mounted, Awake and Start will be executed once and only once.

The update, fixedupdate, lateupdate and ongui functions will be called many times during the game (the number on the right side of the log window indicates the number of times the log information is printed).
Finally, when the game object is destroyed, ondisable and ondestory functions will be called in turn.

2、 Monobehavior life cycle diagram

Here’s another life cycle diagram

3、 Script execution order

In game development, it is inevitable to use many scripts, so how to determine the sequence of calls between different scripts


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("start 1");
    }

    private void Awake()
    {
        Debug.Log("awake 1");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("update 1");
    }
}

Add the above code to test1, test2 and test3 scripts in turn (modify the printed log appropriately) and mount it on different game objects.
Mount test3 first, then test2, and finally test1
The print result is shown in the figure below

The printing result is to print test1 first, then test2, and finally test3.
In fact, the execution order of scripts is related to the order in which they are mounted on the game object. The first to be mounted is executed last, and the last to be mounted is executed first (if readers have doubts, they can constantly adjust the mounting order of scripts to see whether the log printing is consistent with the above conclusion).

It should be noted that regardless of the execution order of multiple scripts, the wake functions of all scripts must be executed before all start functions, and all start functions must be executed before all update functions. Other ordered life cycle functions are similar (as can be seen from the log information in the above figure).

4、 Custom execution order

Sometimes there may be such requirements. The attribute instantiation in script a may need to use the attribute in script B. therefore, when instantiating the attribute of script a, it must be ensured that script B has been instantiated. Of course, we can achieve this by first hanging the script a and then mounting the script B. However, in the actual development, there are many scripts used, so it is difficult to remember the mounting order of each script. Therefore, unity provides a script execution order configuration item to configure the execution order of multiple scripts.

Click a script file arbitrarily in the project panel, and the details of the script will appear in the property panel. Select the script in the upper right cornerExecution Order... , open the interface as shown in the figure below

Click “+” to add a script and set the order value for it. The smaller the order value, the earlier to execute, and the larger the order value, the later to execute

The above is the details of unity script life cycle and execution sequence. For more information about unity life cycle, please pay attention to other relevant articles of developeppaer!

Recommended Today

React explanation – parent component calls child component content [updating]

preface This article belongs to react communication > parent-child communication > parent component calling child component.Scenario of parent component calling child component: Sub components are used in multiple places and need to be encapsulated separately The logic of sub components is heavy, and the cost of using fully controlled mode is high Using parent components […]