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.
So if you have anything that references the SO in your scene, that will load the SO when the thing that references it is loaded, which will call OnEnable.
1 Like
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:
using UnityEngine;
[CreateAssetMenu(fileName = "ScriptableObjectTest", menuName = "Data/ScriptableObjectTest")]
class ScriptableObjectTest : ScriptableObject
{
void OnEnable()
{
Debug.Log("ScriptableObjectTest.OnEnable()");
}
}
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.
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.
1 Like