Search Unity

Question ScriptableObject.OnEnable() called with Play

Discussion in 'Scripting' started by Kyroaku, Oct 27, 2020.

  1. Kyroaku

    Kyroaku

    Joined:
    Oct 29, 2015
    Posts:
    2
    Hello there!

    Every information I've found about ScriptableObject.OnEnable() says that it is called just like Awake() - only when instanciated and not when hitting "Play" button in the editor.

    Meanwhile, I've implemented OnEnable() and it is called every time I hit Play. It has been changed in some newer version? I can't find information about it and I would like to know if I can trust this feature.

    Regards.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Boo-Let likes this.
  3. Kyroaku

    Kyroaku

    Joined:
    Oct 29, 2015
    Posts:
    2
    I have created SO asset object but I am not referencing it anywhere.

    Here is the test class that I have created to be sure:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "ScriptableObjectTest", menuName = "Data/ScriptableObjectTest")]
    4. class ScriptableObjectTest : ScriptableObject
    5. {
    6.     void OnEnable()
    7.     {
    8.         Debug.Log("ScriptableObjectTest.OnEnable()");
    9.     }
    10. }
    11.  
    OnEnable() is called every time I hit play, no matter if it is referenced or not. I'm a bit confused as I was nearly sure it won't work.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    In the editor, everything is a bit weird. Even something like selecting the object in the project window can load the object in the editor.

    The real test will be if you make a build of your game.