Changing a property value in the inspector?

I’m using properties to monitor a value instead of monitoring a variable in Update()
It seems cleaner, and it seems silly to run a monitoring routine in Update() for something that only happens once in a while, even more so if there’s no other reason for a particular script to even use Update()

But I’d like to be able to set that value in the inspector, both before and during runtime - this is especially useful for testing purposes.

The issue is that you can’t serialize properties in the inspector.

I tried working around this by serializing the private backing field, and upon Start(), changing the property to what’s in the backing field. This works, but circumvents the monitoring aspect if that field is modified in the inspector during runtime.

I tried making a seperate field to hold an initial value, and sending that to the property at Start(), but this can’t be used later on, either. (But at least doesn’t bypass monitoring.)

Is there a better way to do this?

Without delving in to writing an inspector I’m not sure there’s a way to write a property to the inspector. I guess you could spoof your display by creating a holding variable (private, serialised) which you update whenever you update your property-protected variable - so update both - and just expose the one you’re not using in your logic. Means that if it’s edited in the inspector, it won’t matter and would update when you next update the value anyway. Could always set the value constantly in the Update method needed, so no amount of edits would matter.

You could write a custom editor that synchronizes the field and the property:

public class SomeBehaviour : MonoBehaviour {
    public float _myFloat;
    public float myFloat {
        get { return _myFloat; }
        set {
            _myFloat = value;
            DoSomething(); //this is what you mean by monitoring, right?
        }
    }
}

And then your editor would be:

[CustomEditor(typeof(SomeBehaviour))]
public class SomeBehaviourEditor: Editor {
    
    public override void OnInspectorGUI() {
        draw
        var script = (SomeBehaviour) target;

        float newValue = EditorGUILayout.FloatField("my float", script._myFloat);
        if (newValue != script.myFloat) {
            script.myFloat = newValue;
        }
    }
}

If you only want to run DoSomething when the game is playing, you can check with Application.isPlaying, and then set _myFloat instead if that’s not the case.

2 Likes

Ok, I think I found another solution:

public class SomeBehaviour : MonoBehaviour {
     bool _loaded;
     [SerializeField] float _myFloat;
     public float myFloat {
          get { return _myFloat; }
          set {
              _myFloat = value;
              DoSomething(); // may also require various initializations performed in Start()
          }
     }

     void OnValidate () {
          if ( _loaded ) {
               myFloat = _myFloat;
          }
     }

     void Start () {
          // initialization stuff for DoSomething(), such as caching scripts, etc.
          _loaded = true;
          OnValidate();
     }
}

This way, _myFloat will accept a value from the inspector, and also whenever it’s changed in the inspector (while the app is running)
However, since OnValidate() runs before Awake() and Start() it might also cause DoSomething() to run before necessary things in Start() have completed. At the end of Start() I can set loaded to true and re-run OnValidate()
Additionaly, I’ll only need one loaded bool, no matter how many properties are used.