Update editorscript if gameobject is manipulated.

I have a editor script that draws a line render between 2 points,
the user can manipulate the whole game object, or 2 handles that are used as start and end point for the line renderer.

Now when the object or handles are moved, the linerender stays in place until I
edit any of its script settings.

Is there a way to know if a object has been modified in any way so I can trigger
my script to update?

1 Answer

1

In your editor class, save the last position of the object. If it’s changed, update the line renderer.

private Vector3 lastPos;
private GameObject _target;

void OnEnable()
{
     _target = (GameObject)target;
     lastPos = _target.gameObject.transform.position;
}

void OnSceneGUI() {
     if(_target.gameObject.transform.position != lastPos) {
          // Update your end point positions
     }
     // Draw your lines
}

thanks, It only works when the object with the script attached is selected. Any way to make this work when a child is moved? Or will I have to write separate scripts for that?