- Home /
Same script execution order issue
Basically I want to know about script execution logic. At present same script added into 5 different game object but script is not executing in sequence though object exist in game in sequential order.
Here is script execution order reference image:
I want script to be executed in 1 to 5 order as like their object created/arranged in hierarchy. So please share your thought regarding this.
Script Execution Order Settings https://docs.unity3d.com/$$anonymous$$anual/class-ScriptExecution.html
Here we are talking about same script execution order on multiple objects. I want that in a particular order.
Answer by DaDonik · Jul 10, 2017 at 10:50 AM
I assume you are using the Update method to print to the console. There is no guarantee in which order any of your scripts will be updated.
What you can do is to create a new script, called TailHandler for example, which holds references to your other scripts. Simply rename the Update method of your current script and invoke that method from the TailHandler. Now you are in charge of the execution order!
So as per your saying I have two ways for solution, i) use different script for execution ii) use one update method and then call all child object code from that
basically each attached node contains same script to follow the upper node so what is better approach?
In the end the right solution is the one you feel comfortable with. Personally i don't like to be dependent on the hierarchy, except for UI elements, but that is mostly a personal preference.
Basically I thought like that each child node have script which doing task of following its parent node so in this case execution order matter otherwise you will see a gap between some child nodes.
Are you making a snake clone?
Then it would make most sense to have a handler script that has a list of references to all the nodes. I would assume that each node has a reference to it's parent and one to it's child.
Iterate over the list and update each node.
Add new nodes to the back of the list, which will automatically make them update in the right order. Set the parent of the node to the previous element in the list and set the child to null.
When removing a node somewhere in the list, remember it's parent and child nodes. Then access the parent and set it's child node to the one you remembered.
What you need is pretty much a linked list. You can look that up on google. There should be plenty of examples of linked lists in C#.
Now I think simplify above of your answer after seeing my question properly so I can make this correct and become helpful to others.
Your answer
![](https://koobas.hobune.stream/wayback/20220612125917im_/https://answers.unity.com/themes/thub/images/avi.jpg)