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
.
Then, within Project Settings
, open Script Execution Order
.
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.
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:
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.
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 for providing hints for this article.
Reference Site ①: Can Unity Scripts run the Update methods in order? - Unity Engine - Unity Discussions
Reference Site ②: Script Execution Order - Unity Manual
Note: The English translation for メゾット
is Method
, not Mesos
. Please be aware of the typo in the title.I've missed it :(