Search Unity

Question ScriptableObject intermittently doesn't Awake() or OnEnable() in editor

Discussion in 'Editor & General Support' started by MrLucid72, May 19, 2022.

  1. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    996
    Unless I actually click on the ScriptableObject, Awake() is never triggered.

    I tried all kinds of things, like serializing it in `Preloaded Assets`.

    The only workaround was to serialize it in my first scene, but that's not-so-classy.

    Any ideas?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    Awake/OnEnable/OnDisable for scriptable objects are only called during Domain reloads whilst in the editor. Are you expecting them to be called at other times?
     
  3. AnonnyMoose

    AnonnyMoose

    Joined:
    Nov 28, 2014
    Posts:
    30
    Well yes, I expect any class that has an Awake/etc to be called at all times editor and in runtime. If its only to be called in the Editor, it should have its own EditorAwake etc calls.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    That's not what I said though. I only described when they're called in the editor. They are still called at runtime, just with different behaviour/timing.

    Read the documentation.
     
  5. AnonnyMoose

    AnonnyMoose

    Joined:
    Nov 28, 2014
    Posts:
    30
    You made it seem like they only get called in the editor. Either way, Awake calls are *not* happening in runtime even though the documentation says they should be.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    You're just misreading my post from a year ago.

    Nonetheless Awake is a bit weird with scriptable objects and ultimately useless I think. You should just use OnEnable/OnDisable, and beyond that, rely on scriptable objects having some form of initialisation as little as possible.
     
    briant_001 likes this.
  7. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    146
    Just have Init() method on each SO and have _initialized boolean check inside Init()..

    then call Init() everytime you use SO (inside Awake/Start of other MonoBehaviors)..