Unity - Good to Know! ~How to Execute Update Functions in Order~

### Inevitabilities in Game Development
One day, whilst coding, the following occurred:
> "Alright, I’ve written the script to make the object follow along!"
> Then I execute it.
> "Oh no! It’s **bugged**!"
> The object isn’t perfectly aligned with the target object—it’s slightly off.

This kind of programming frustration and the **bugs** it causes are something we’ve all experienced at some point, haven’t we? (Pressure intended)
In this case, the bug arises because the script’s `Update function` is not executed in the order the programmer originally intended.

Is there a way to resolve this issue?

This time, I’ll explain how to execute the `Update function` in a specified order.

* * *
### The Main Topic ~Executing Update Functions in a Specified Order~
First, open `Project Settings`.
To do this, go to `Edit` > `Project Settings`.
![Screenshot (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4042593/585ee24c-0c2b-4a3a-afcc-88eef7e2938c.png)
Then, within `Project Settings`, open `Script Execution Order`.
![Screenshot 2025-03-29 114201.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4042593/fb0bcb11-a9cb-4c3d-b282-265ddd5b219d.png)
Inside `Script Execution Order`, there are processes pre-set for making the screen more realistic or updating the UI.

From here, add the scripts you want to execute in order.
Click the `+ ▼` button at the bottom of the list of processes, and add the scripts **preferably in the intended order**.
![Screenshot 2025-03-29 115802.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4042593/8f34dec7-aaf6-4a5f-a5a9-98f5e2940d18.png)
In my case, I want to execute the scripts in the following order: `SplineSystem.cs` > `RearWheel.cs` > `TrainTransform.cs`, so I’ve set it up accordingly.

Once configured, **don’t forget to press `Apply`**.

* * *
### What Do the Numbers Mean?
Speaking of which, in the earlier image, there were numbers like `100` and `200`. What do they signify?

Let’s refer to the official documentation for an explanation:

https://docs.unity3d.com/Manual/class-MonoManager.html

Here’s what it says:
> The assigned numbers indicate relative order. Unity executes the list from top to bottom (starting with scripts that have large negative order numbers and ending with those with large positive order numbers).

In other words, **Unity executes processes starting from the ones assigned the smallest numbers to those with larger numbers**.

However, it also mentions this:
> Order numbers are arbitrary and do not represent a physical quantity.

This means that regardless of how you number them (e.g., `0` > `100` or `1` > `2`), the execution order remains unaffected.

:::note info
You can also drag and drop the processes within the list to change their order.
:::

***
### Summary
In this article, I explained how to execute the `Update function` in a specified order.
By using Script Execution Order, you can arrange scripts in the desired execution order.

Lastly, I’d like to express my gratitude to [Meredoth](https://discussions.unity.com/u/meredoth/summary) for providing hints for this article.

***
Reference Site ①: [Can Unity Scripts run the Update methods in order? - Unity Engine - Unity Discussions](https://discussions.unity.com/t/can-unity-scripts-run-the-update-mesos-in-order/1619144)
Reference Site ②: [Script Execution Order - Unity Manual](https://docs.unity3d.com/ja/current/Manual/class-MonoManager.html)
Note: The English translation for `メゾット` is `Method`, not `Mesos`. Please be aware of the typo in the title.I've missed it :(