Is there some sort of method/function that is called by Unity when creating (right-click, Create) a ScriptableObject? I need the method/function to be called just once when it is created. thanks
ScriptableObjects have OnValidate, just as MonoBehaviours do.
This function runs whenever there is a change to the object’s serialized values which is more often than you’d like, so combine this with a “hasBeenInitialised” type flag to make sure you only run it once.
public class YanfundisScriptableObject : ScriptableObject
{
[SerializeField, HideInInspector] private bool _hasBeenInitialised = false;
[Conditional("UNITY_EDITOR")]
private void OnValidate()
{
if (!_hasBeenInitialised)
{
// do your initialisation :)
_hasBeenInitialised = true;
}
}
}
Reset() is called on creation and on selecting ‘Reset’ in a ScriptableObject’s context menu.
Since this has been necro-ed, for editor tooling I suggest using an AssetPostProcessor instead: Unity - Scripting API: AssetPostprocessor
Specifically AssetPostProcessor.OnPostprocessAllAssets is a good way to initialise scriptable objects that need something done just once when they’re created/