- Home /
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!
For each variable declared inside a script, it repeats once a frame and resets itself under the influence of Update() too. So just put them out of onGUI(). If that doesn't work then try using a script to gather data then recline it back to the script each frame for processing.
my variables are global, but are not updated on Update(), only on onGUI(), I click over the text field and it doesn't let me change the values.
I don't think I fully understand your second sugestion.
Answer by DaveA · Apr 23, 2012 at 03:32 PM
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;
}
I've tried your solution but I can't get it to work. The main problem is that initially I want for the actual values to appear on the textfields. Then I want to be able to edit them, to put any value I want and the object assume it in real time.
It's kinda like the unity's editor. We have an object X position and we can edit it by changing its value.
Thanks for all the help so far
I've forgot to add, that I still cannot change the value on the text fields :(
Answer by teebird · May 02, 2012 at 09:51 PM
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.
If I start x,y or z with any value, I can than edit the object with the textfield using your methods.
The problem is that initially I want the textfield to show the gameObject.transform.position.x, and update this variable via texfield. I've used the Start() function to give variable x the gameObject.transform.position.x value, but than I can't edit the text field.
So you've grabbed the position of the object under start, and then set it to a string and put it in your text box the way I said? And you can't edit it? That shouldn't make a difference... Just remember that in x = float.Parse (GUI.TextField (Rect (10,10,100,20), x); x (or whatever you want to call it) HAS to be by itself at the end so that you can edit x, since x.ToString is not a variable and therefor changing the text field can't change x. Unless you have x being set somewhere else in your code under OnGUI or Update, I don't know why you wouldn't be able to change it. Being able to see your code would help.
I think I'm following all your instructions, and still cant get there. Anyway here's the code:
var x:String= "0"; var xpos : float; var globalPosition;
function Start() { xpos= gameObject.transform.position.x; x=xpos.ToString; }
function OnGUI() { x = GUI.TextField (Rect (SBtopx+25, SBtopy+40, 60, 20), x); globalPosition = new Vector3(float.Parse(x),float.Parse(y),float.Parse(z)); gameObject.transform.position=globalPosition;
xpos=float.Parse(x); }