- Home /
Unity Editor How to Detect a Change of a Specific Component Field
I currently have a component which has two public fields, an "X" and a "Y" position which each represent a cell within a grid. When either of these values change, I want to move the gameobject's position to be centered on the targeted cell.
So I want to create a custom editor which will change the gameobject's position when it detects that either the "X" or "Y" value has changed. I know how to do this with a custom built gui, however, these two fields are both created inside DrawDefaultInspector() and as a result, I cannot directly access them. How do I detect a change in either of these individually when they are created and handled by default?
Thank you.
Answer by Larry-Dietz · Dec 24, 2017 at 11:41 PM
I'll be honest, I don't write editor scripts, but I do have several scripts that react to changes in the variables.
What I would recommend trying would be to declare the fields you want to monitor as full properties complete with getter and setter. Then in the setter, in addition to setting the variable, do whatever you want to do when the value changes...
i.e.
private float _x;
public float X {
get {return _x;}
set {
_x = value;
//Do whatever else you want done when X changes here
}
}
Hope this helps, -Larry
Thanks Larry. This is a good idea and will certainly work with a custom editor. However, the main downside is that Unity does not automatically handle properties which means I will need to create the GUI for all of them from scratch. I will probably take this approach if there is no standard unity way of doing what I want to do.
Again, I admit to knowing nothing about editor scripts, but I know on the properties I have used in scripts, adding [SerializeField] to the declaration does cause them to show up in the inspector.
Your answer
Follow this Question
Related Questions
EditorGUI like light explorer window 1 Answer
What is a GUILayoutOption[] 3 Answers
How To Best Match Editor GUI Content Heights 0 Answers
How to communicate between two editor scripts? 0 Answers
Custom Inspector Preview Buttons 0 Answers