I use a "framework" class that is set to run before all the other scripts (-300). This class has an Awake method. I also use another normal monobehaviour which defines the OnEnable method. Once the application runs, the OnEnable of the second monobehaviour is called before the awake of the framework class. Is this the intended behaviour? |
It's rather complicated. A simple test with 3.5.2 revealed, most concurrent functions (well, at least the ones I tested: Awake, Start, OnEnable, FixedUpdate/Update/LateUpdate) abide by the execution order defined for the scripts. The execution order of OnLevelWasLoaded is not affected by that, and therefore cannot be influenced by the user. This could be considered a bug. The order of the four methods of a script related to initialization is always:
However, if your script was disabled in the first place(via Script.enabled=false), this order changes to:
In addition, note that Awake() and OnEnable() calls are connected/interleaved. Meaning, assuming a particular, user-defined execution order A and B with A*<*B,
In particular, this means that OnEnable() of type A will be executed before Awake() of type B, while OnEnable() of type B will be executed after Awake() of type A. This overview explains it more clearly:
EDIT: Hm, this is a total mess. If DontDestroyOnLoad() is activated for such a script, this will geteven more complicated, and the order changes yet again to:
EDIT2: In addition, when DontDestroyOnLoad() is active, the user-defined execution order is no longer abided by!, neither by OnEnable(), nor by OnDestroyOnLoad(). EDIT3: WAH! I'm gonna stop testing now, this is a neverending story... As @Noisecrime noticed, there is actually another bug, where user-defined execution order is overriden if the script has an OnEnable() function! Script A has OnEnable, Script B has not:
Script B has OnEnable, Script A has not:
answered Jun 27 '12 at 03:38 PM Wolfram |
And note that Awake is called even on a disabled component, but not if the game object it is attached to is inactive. (This may or may not seem obvious, it wasn't to me ... ;-)