Execution Order alternative

Unity has the undocumented attribute [DefaultExecutionOrder()] which is an alternative way to instruct Unity to place a script in a particular position in the execution order. Passing an integer that is less than zero will instruct unity to execute the script before any other user defined scripts. To use, simply place before the class delcaration:

[DefaultExecutionOrder(-1)]
public class SomeScriptThatNeedsToExecuteFirst : MonoBehaviour
{
    // Super important code here
}

Adding the script in the Project Settings>Execution Order tab may be a better way to go, as you can quickly look at the chart and see the execution order. However, the attribute is a nice way to test if adjusting the execution order of a particular script will solve any raise conditions you may be experiencing.

just thought I’d share,
Cheers

2 Likes

Yup that’s a great tip if you want to do it all in the code.

1 Like

It also makes the classes more portable, as if you import them into another project, you won’t have to manually set the execution order in the Editor in your new project.

1 Like

Privacy & Terms