Changing variables in real time.

Hi!

On my project when I select an object, some of it’s properties (scale, rotation, position) appear on different text fields. My purpose is to change the values on the text fields, by hand, so the objects assume the new values in real time.

I understand that the main problem is using the onGUI() function, once it updates the values frame by frame, but I can not think of a way of pulling this off.

Thanks in advance!

Something like this. If you don’t want it to update in OnGUI, you can have ‘lastX lastY lastZ’ variables which you set before the TextFields, then compare to x,y,z after TextFields to see if they change. If any change, then assign to global position and/or transform.

var globalPosition;
var trn : Transform;
private var x = 0;
private var y = 0;
private var z = 0;

function OnGUI()
{
 x = float.Parse (GUI.TextField (Rect (10,10,100,20), x.ToString());
 y = float.Parse (GUI.TextField (Rect (110,10,100,20), y.ToString());
 z = float.Parse (GUI.TextField (Rect (210,10,100,20), z.ToString());
 globalPosition = new Vector3 (x,y,z);
 trn.position = globalPosition;
}

The problem with this
x = float.Parse (GUI.TextField (Rect (10,10,100,20), x.ToString());

Is that using the method x.ToString means you’re not using a variable in your display. Thus, when someone types it in, you aren’t changing the value of x as it should be. If you were to use instead

var globalPosition;

var trn : Transform;

private string x = “0”;

private string y = “0”;

private string z = “0”;

function OnGUI()
{

x = float.Parse (GUI.TextField (Rect (10,10,100,20), x);

y = float.Parse (GUI.TextField (Rect (110,10,100,20), y);

z = float.Parse (GUI.TextField (Rect (210,10,100,20), z);

globalPosition = new Vector3 (x,y,z);

trn.position = globalPosition;
}

Then you simply have to parse x, y and z back to int/float values when you want to handle them. Dave was close, but I do believe this should work.