Transfer inspector value into script variable (C#)

Hi there.

For script optimisation, I’m looking for a way to transfer values from an object to the an assigned script.

Usage: My script contains two positions (Vector3), a “first-position” and a “moved-position”.
Now I want to put an objects position into the first Vector3, then move it inside the editor, and then assign the new position to the second Vector3, without typing in every single transform value by hand/copy+paste.

Is there a possibility to assign the values to a variable by a button in the inspector?


public Transform yourObject;

private Vector3 firstPosition;

void Start(){
    firstPosition = yourObject.position;

Drag-Drop your object in yourObject field through inspector and fetch its position in your Vector3

If you just wan a button that does that, you can create an InspectorEditor for your script and use the GUILayout.Button() to create a button.

Otherwise, if you just need a reference, you should go with aditya’s answer.