- Home /
Detect when a property is changed on a ScriptableObject Asset
I have added a menu command that creates a new ScriptableObject and saves it as an asset. I have also created a MonoBehaviour that has my ScriptableObject as a property. I can add my behaviour to a game object and select my asset using the asset selector. My problem is that when I change a value on my asset, how does my behaviour detect the change and update accordingly?
As a temporary work around I have am storing the old value of the scriptable object properties in my behaviour and then checking each frame to see if they have changed. This is how I detect that the user has edited the behaviour properties. It works, but is starting to get to be a lot of book keeping.
Answer by MattRix · Jul 27, 2015 at 02:53 PM
You can use OnValidate() in ScriptableObjects, but Unity forgot to document it for some reason.
According to this, OnValidate kinda sorta works with ScriptabeObject atm. Can you provide a working sample? I can't seem to make this work on my end.
https://feedback.unity3d.com/suggestions/onvalidate-callback-for-scriptableobjects
This is awesome, thank you! I wasn't even thinking about just testing it!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CreateAsset$$anonymous$$enu()]
public class ColorScheme : ScriptableObject {
public Color dialogBackground;
public Color sceneTransition;
public void OnValidate() {
Debug.Log(Time.time);
}
}
Thats perfectly working example.
Ahhh yes thank you! I didn't see it as part of the Unity messages recognized by Visual Studio so I was just like ah it doesn't work.. lol so glad it does though!
Your answer
Follow this Question
Related Questions
How to reference variable in ScriptableObject(.asset files)? 1 Answer
Prefab as a subasset of a ScriptableObject 0 Answers
Why does my List overwrite itself when using .Add? 2 Answers
Auto removing entries form a scriptableobject list when asset is deleted. 0 Answers
How to make game moddable? 1 Answer