- Home /
GUI.Textfield function won't update variable
Hello,
I have this tad bit of code, but the variable in the textfield won't change because it's thru the function.(I think this is the reason)
var tft : String;
function OnGUI () {
Block ("Texture", "texturefile=", tft, 25, 85);
}
function Block (lblk : String, wblk : String, blk : String, vx : int, vy : int) {
GUI.Label (Rect (vx, vy, 150, 20), lblk);
blk = GUI.TextField (Rect (vx + 90, vy, 150, 20), blk);
if (blk != "") info += wblk + blk + "\n";
}
If anyone knows a workaround for this, I'd be very greatful.
Answer by jahroy · Oct 15, 2011 at 05:48 AM
The content of the TextField isn't changing because you never store the return value of GUI.TextField anywhere.
GUI.Textfield returns the string in the text field every frame (so you can do something with it). For it to get updated when the next frame happens, you need to store it somewhere.
Using your code as an example:
There's no need to pass tft as a parameter in the function named Block.
You can just put the following inside of Block:
tft = GUI.TextField(someRectangle, tft);
Think of your script as a class. The variable tft is a member (or property) of that class, so functions (or methods) inside the class have access to it.
In your code the return value of GUI.TextField is never recorded. The unchanged value of the variable tft is continually re-assigned to the contents of the TextField every frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
function GUI.DrawTexture with OnMouseEnter/Exit to change texture all within script? 0 Answers
Assigning two function to a GUI button? 2 Answers
GUI.Window function with variables? 3 Answers
How to make the tab key to show/hide GUIlayer 1 Answer
Button is unable to grab functions with two parameters? 0 Answers