I have a script that I want to run before any other script. I want to change its execution order settings. I could do that using the Script Execution Order Settings option from within Unity, but I want to do this programmatically.

The reason I want to do this programmatically is because the script has an abstract class, and I want to avoid requiring the user to change the execution order every time they extend the class. Changing the execution order of an abstract class is not an option in the Script Execution Order Settings. My abstract class has reference to every class that extends it.

How can I change the execution order of a MonoBehaviour? An example of what I am looking for would be along the lines of:

MonoBehaviour myBehaviour = GetMyMonoBehaviour();
myBehaviour.ExecutionOrder = -100;

You can set the order via scripts using the MonoImporter class, specifically, the method SetExecutionOrder:

MonoScript myScript = MonoScript.FromMonoBehaviour(yourMonoBehaviour);
MonoImporter.SetExecutionOrder(myScript , -100);

However, I haven't seen this in the documentation post 5.0, so it may be depreciated if you're using the latest version.


If you're running this script every frame, you could consider using Update and LateUpdate across different scripts. Depending on what you're looking for, you might do the "early" calculation at the end of a LateUpdate call as described here.

  • \$\begingroup\$ If you happened to downvote, please explain why so I can leave better answers. \$\endgroup\$ – Stephen Schroeder May 31 '16 at 19:06
  • 2
    \$\begingroup\$ I didn't downvote, but I'd say it was because you didn't attempt to answer the question being asked. You made a recommendation for an alternative solution (which seems like it might be more work than the alternative of manually configuring this setting that was suggested by OP). That makes this answer more of a comment than an answer. If you updated it to include information about the question being asked (as well as your alternate solution), that would probably be more useful. \$\endgroup\$ – MichaelHouse May 31 '16 at 19:21
  • \$\begingroup\$ When you say "updated it to include information about the question being asked", do you mean how my answer is relevant, or requesting more information, or something else? \$\endgroup\$ – Stephen Schroeder May 31 '16 at 19:25
  • \$\begingroup\$ I mean, you'd say something like, "This is how you would do that, and here's an alternative suggestion..." or "I don't think that's possible, you might try this instead...". Right now, your answer hasn't addressed "How can I change my script execution order programmatically?" \$\endgroup\$ – MichaelHouse May 31 '16 at 19:29
  • \$\begingroup\$ So should alternative solutions generally be comments on questions instead of answers? \$\endgroup\$ – Stephen Schroeder May 31 '16 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.