Reputation: 303
How do I write a logic in script to detect if the object field has been changed in inspector window? I am using a script that executes in editor as well as play mode and I am altering OffsetPosition
in the inspector window. The offset position is then copied to my GameObject. I want a function to detect if the gameObject has been changed in inspector window.
public GameObject Offset;
public Vector3 OffsetPosition;
public bool GameObject_NotChanged;
void Update () {
if(GameObject_NotChanged == true)
{
Offset.transform.position = OffsetPosition;
}
}
void GameObject_Changed()
{
GameObject_NotChanged = false;
}
Upvotes: 0
Views: 780
Reputation: 90590
You could use OnValidate
This function is called when the script is loaded or a value is changed in the Inspector (Called in the editor only).
like so
public GameObject Offset;
public Vector3 OffsetPosition;
private GameObject _lastOffset;
private Vector3 _lastOffsetPosition;
private void OnValidate()
{
if((Offset != _lastOffset || _lastOffsetPosition != OffsetPosition))
{
if(Offset)
{
Offset.transform.position = OffsetPosition;
}
_lastOffsetPosition = OffsetPosition;
_lastObject = Offset;
}
}
Upvotes: 2