Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skitch · Apr 23, 2012 at 02:29 PM · onguitextfieldrealtime

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · Apr 23, 2012 at 02:35 PM 0
Share

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.

avatar image skitch · Apr 23, 2012 at 02:58 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skitch · May 02, 2012 at 07:43 PM 0
Share

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

avatar image skitch · May 02, 2012 at 08:43 PM 0
Share

I've forgot to add, that I still cannot change the value on the text fields :(

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skitch · May 04, 2012 at 03:43 PM 0
Share

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.

avatar image teebird · May 04, 2012 at 08:29 PM 0
Share

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.

avatar image skitch · May 14, 2012 at 07:53 AM 0
Share

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); }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger 'if' statement while in TextField 2 Answers

EditorGUILayout.TextField returning empty string 3 Answers

Android Bug related to drawing TextField after a Box which is only in a Repaint event 0 Answers

TextField moves/jumps when focused 0 Answers

Gui.textfield empty deletes. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges