How to make monobehavior script component trigger code when field changes?

I would like to give the user (ie. the developer using my tool) the ability to define a 2D grid for their UI canvas. I plan on having them add a monobehavior script to their “grid” gameobject, and the inspector should show two int properties: Width and Height. If they were to enter the number 8 into both fields, I would like to react immediately to the user’s changes and display an 8x8 grid in that gameobject, via the use of a tiled image component (for each individual square) and a rect transform to define the overall size. I have everything in place for this, except for the part where it dynamically changes the grid’s size upon user input. I understand that getters/setters are not allowed in the inspector, so my question is: what’s the alternative? How do I respond to the user’s changes so that I can immediately update the grid accordingly?

OnValidate is a great method for checking inputs.
Otherwise you can also use custom inspectors, but that’s more complex

1 Like
public class Test : MonoBehaviour
{
    public int ID = 0;

    private void OnValidate()
    {
        //Do Something
        Debug.Log(ID);
    }
}