Search Unity

Call method before ScriptableObject's OnEnable

Discussion in 'Scripting' started by Koval331, Aug 11, 2019.

  1. Koval331

    Koval331

    Joined:
    Feb 3, 2019
    Posts:
    114
    Is there a way to execute a script before OnEnable of the ScriptableObjects are called? I tried RuntimeInitializeLoadType.BeforeSplashScreen and RuntimeInitializeLoadType.BeforeSceneLoad, but they are called after OnEnable.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The short answer is "not that I know of". The long answer is that I believe Awake and OnEnable are only called in builds when an asset in the scene references that ScriptableObject (or if it's in Resources, maybe?), causing it to load in the background immediately. In that case, all of the ScriptableObjects referenced will have Awake and OnEnable called before the first Awake is called for any scene objects (and as you've noted, before RuntimeInitializeOnLoad, though that's new information to me). The only solution that comes to mind is to load a scene that doesn't reference those ScriptableObjects first, do your static initializations with RuntimeInitializeOnLoad, then load the first proper scene afterward.

    I'm unaware how much that changes in the editor with Play Mode active, instead of in builds though.
     
    Koval331 likes this.