Monthly Archives: February 2017

Unity3D and OnValidate

So, I came across MonoBehaviour method earlier today that I’d previously missed:

void OnValidate()

This is called when a script is loaded, or (editor only) when a parameter is changed via the inspector.  I’m using this to allow me to set a property that has a getter/setter via the inspector by using an extra variable.   I set the extra copy in the inspector, and then use code in OnValidate to copy it to the setter method.

I’m also going to use it instead of the Update method for my editor scripts – Update is called every time something changes in the editor, OnValidate, only when something on that script changes – much more effective.