Search Unity

Same Method on Multiple Script Order prediction?

Discussion in 'Scripting' started by laurelhach, Aug 30, 2017.

  1. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    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.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  3. Bantaru

    Bantaru

    Joined:
    May 11, 2016
    Posts:
    59
    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.


     
  4. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    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 :)
     
  5. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    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?)
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.

    Code (csharp):
    1.  
    2. [SerializeField]
    3. private UnityEvent mouseDown;
    4.  
    5. private void OnMouseDown()
    6. {
    7.     if (mouseDown != null) mouseDown.Invoke();
    8. }
    9.  
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
    Suddoha and ADNCG like this.
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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. :D

    I'd recommend to ensure the order of processing by yourself, even if Unity stays consistent with the top-to-bottom detail.
     
  10. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    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:
    Code (CSharp):
    1. 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!