Defiantly not.
It’s okay for me to use Debug.Log, I didn’t know that:
I found this Unity forum. That kinda explained the issue, but not solving it.
Documentation says “Awake is called as the ScriptableObject script starts.” but no one told you that SO script starts when you hit Play in the editor
Scriptable objects are instantiated in edit mode (as all other referenced assets) and not re-instantiated or reloaded when you start playing in the editor, this allows fast start without loading everything every time.
So documentation is true, while your expectations are false. Sorry.
P.S. I was blocked by this feature too and I do understand your pain.
There are also some solutions:
Im not sure if will it run in the build correctly or not.
[InitializeOnLoad] public class Test : ScriptableObject { void OnEnable () { Debug.Log ("Unity editor has just started up!"); } }
Now the method is called whenever the ScriptableObject is created and whenever Unity is launched.
Also, they come up with that:
using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif [InitializeOnLoad] public abstract class ManagedObject : ScriptableObject { abstract protected void OnBegin(); abstract protected void OnEnd(); #if UNITY_EDITOR protected void OnEnable() { EditorApplication.playModeStateChanged += OnPlayStateChange; } protected void OnDisable() { EditorApplication.playModeStateChanged -= OnPlayStateChange; } void OnPlayStateChange(PlayModeStateChange state) { if(state == PlayModeStateChange.EnteredPlayMode) { OnBegin(); } else if(state == PlayModeStateChange.ExitingPlayMode) { OnEnd(); } } #else protected void OnEnable() { OnBegin(); } protected void OnDisable() { OnEnd(); } #endif }
And when you inherit from it, you implement the OnBegin and OnEnd methods. It runs on starting unity editor, on entering/exiting playmode and on starting your built application.
I will be happy to hear your opinion about that solutions. May be better approach would be to use the method that I mentioned earlier.
UPD:
I will use CalculateDelta because other approaches fix only Scriptable Object and not classes in it.