Editor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
Use this callback to validate serialized fields and enforce data constraints in response to Inspector edits. This is the primary intended use case for OnValidate. OnValidate can also be called at various stages during the Editor's normal operation, such as loading scenes, importing assets, building a Player, and entering Play mode. Because of this, don't treat OnValidate as a general-purpose callback for all property changes or Editor-side logic.OnValidate doesn't save anything itself. Values it assigns to serialized fields are written to disk only when the containing scene or asset is saved for another reason, such as an edit you make. When Unity calls OnValidate while loading or importing an object, Unity clears the object's dirty state afterwards, so that a loaded object always appears clean. Values assigned during a load aren't scheduled for saving, even if you call EditorUtility.SetDirty from OnValidate.
Don't rely on OnValidate to persist computed values, especially in Prefabs. A Prefab Variant stores only explicitly edited values as property overrides, so a value that OnValidate assigns is not written to the variant's file, including when the variant is loaded, imported, or updated because its base Prefab changed. To persist a computed value in an asset, assign it from an explicit edit, for example from an AssetPostprocessor.OnPostprocessAllAssets callback that saves the Prefab using PrefabUtility.SavePrefabAsset.OnValidate can be called often when the user interacts with an Inspector in the Editor. It can also be called from threads other than Unity's main thread, such as the loading thread. For these reasons, only use OnValidate to validate the data that changed. Don't use it to do other tasks such as creating objects or calling other non-thread-safe Unity API.
You can't reliably perform Camera rendering operations from OnValidate. Instead, add a listener to EditorApplication.update, and perform the rendering during the next Editor Update call.
Additional resources: EditorApplication.update, EditorApplication.delayCall.
using UnityEngine;
public class Health : MonoBehaviour { [SerializeField] private int maxHealth = 100;
[SerializeField] private int currentHealth = 100;
// Called in editor when values are changed in Inspector private void OnValidate() { // Ensure maxHealth is at least 1 if (maxHealth < 1) maxHealth = 1;
// Clamp currentHealth between 0 and maxHealth currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth); } }