- Home /
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?
Greetings
Answer by chillersanim · Oct 06, 2016 at 07:42 AM
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.
https://docs.unity3d.com/ScriptReference/GUILayout.Button.html
Otherwise, if you just need a reference, you should go with aditya's answer.
This led the way! Thx.
For anyone else: ADDING BUTTONS TO A CUSTO$$anonymous$$ INSPECTOR: https://unity3d.com/learn/tutorials/topics/interface-essentials/adding-buttons-custom-inspector
Answer by aditya · Oct 06, 2016 at 06:51 AM
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
That doesn't help; I can't go over the Start or Awake or any runtime function, as long as superposition isn't working in unity.
Your answer
Follow this Question
Related Questions
Link a value to another value in the Inspector 1 Answer
Arrays and the inspector 1 Answer
Unexpected values at the inspector when initializing member values from built-in arrays 0 Answers
Inspector won't update with new variables.,Variables don't seem to update on inspector 0 Answers
Are inspector values supposed to override [HideInInspector] code? 1 Answer