Hi Guys,
Not really sure how to actually title my issue, but here is the question:
I have two scripts that contains OnMouseDown() Method on the same prefab.
I would like to know if there is a way to know which script will run OnMouseDown() first?
In my situation, it seems to always be the same order, but can I rely on that?
Thanks!
PS: I tried to find other topics about that, but couldn’t find anything.
If this is for two different script files, you can manually set the execution order by class (script) in Edit → Project Settings → Script Execution Order.
If the two objects have two instances of the same script you cannot rely on a particular execution order for any Unity-called methods.
Done a quick test to confirm my thought. If You have two scripts with same call on one object, they will execute from top to bottom.
Images
Thanks Guys,
StarManta
Thanks, but I would like not to start messing with the script execution orders at least for now 
To be more precise: I have 1 object with two scripts.
Script A has OnMouseDown() and Script B has also OnMouseDown()
Both scripts are on the same Prefab.
I obviously could call B from A in OnMouseDown, but I was trying to avoid dependencies between scripts.
Bantaru,
I tried to do what you suggested, but I couldn’t confirm your thoughts when I tried it.
I will try again.
Thanks!
Edit: Bantaru, you are actually right 
So I don’t know if I should post another thread but how can I make sure both scripts keep the same order in the component section? Is there a way to force that (so if by mistake they don’t have the same order I can check by script?)
This is undefined behavior so it could change without warning; don’t rely on it.
You could create a single script with OnMouseDown that raises an event that other scripts can listen for.
[SerializeField]
private UnityEvent mouseDown;
private void OnMouseDown()
{
if (mouseDown != null) mouseDown.Invoke();
}
I think it might be defined behaviour. Because it’s defined that’s the order of the post effects stack. In short, it’s been used by Unity themselves to organise order that post effects are processed in so it must logically work top to bottom.
Good point. I’d still be hesitant though because this seems like it falls in the realm of Unity’s magic method processing. Until the documentation for stuff like Awake and Start comes out and explicitly says “These are processed top to bottom in the order they appear in the Inspector” I’m not going to say to rely on this behavior in this scenario.
2 Likes
There are other reasons to not rely on that.
The setup might seem clear and obvious for now, in the future you may (for any reason) need to re-build the prefab, or other prefabs and you might not remember that little detail. Or you want to build one object with the components at runtime. This can be fun… Let the party begin. 
I’d recommend to ensure the order of processing by yourself, even if Unity stays consistent with the top-to-bottom detail.
Thanks for all the answers.
I will probably not rely on the top to bottom script ordering. Even if I like it, like Suddoha says I might forget about it at one points.
There was an option probably in a previous version of Unity (I can’t find it with 5.6.3) where you could use this:
UnityEditorInternal.ComponentUtility.MoveComponentDown (someComponent);
But apparently it doesn’t work anymore. If there is still a way to access it, I would make sure to create a tool to allow me to check all prefabs.
Thanks!