Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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,724
    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,724
    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.