- Home /
Text object can't be edited
I have an input field and event that calls a function when I press enter.
public Text theText;
I take the Text part of the input field (connect them in editor), display the content and set new value to some string.
Debug.Log(theText.text);
theText.text = "new text";
I get the contents displayed, but the value isn't changed.
Why am I able to read but not write to it? And how do I fix this?
EDIT: To clarify, I'm talking about the new ui system. I tried creating a new scene, with just that input filed with a script (attached to it) that contains the 2 lines I posted earlier (I connect the text field, child of the input field, to "public Text theText;" in editor). And it's not changing text, just showing it. I suppose I could create a GUI.TextField in OnGUI(), that way I could surely change the text value. But the current way I'm trying now worked fine few days ago, I don't get what could not be happening..
Are you asking why the name of the GameObject hasn't changed?
http://docs.unity3d.com/ScriptReference/UI.Text.html
shows that Text.text is the variable string for what is displayed, it doesn't influence anything else.
Do not modify the name of the actual object itself as that is something even advanced programmers use with extreme caution and I doubt you can find a dozen of them that actually do.
@sniper43 this has nothing to do with the object name, it's about the text that is displayed in the input field
@zoran404 There is no input field here and I might just be getting more confused.
Text is a simple GameObject that displays text, it shouldn't take input.
Are you using something like this? (Don't trust me on this, haven't had to use an input field yet) http://docs.unity3d.com/ScriptReference/GUI.TextField.html
Answer by zoran404 · Jan 23, 2015 at 07:03 AM
For those who are interested the problem was that I tried to directly edit the UI.Text object, which was a child of an UI.InputField object.
When I replaced public Text theText;
with public InputField theText;
, connected the actual InputField to that variable, not it's child the Text object, tried to change it with theText.text = "new text";
it worked fine.
Answer by Mmmpies · Jan 21, 2015 at 08:52 PM
Your code looks OK so if this doesn't work then try posting more of the script so we can see what's going on, that said...
public variables are overridden in the inspector, try setting it to private, call the Text object something unique and using GameObject.Find to find it e.g.
private Text theText;
//in start
theText = GameObject.Find("uniqueTextName").GetComponent<Text>();
example in C#
Answer by AscendDev · Sep 16, 2017 at 12:26 PM
Just as a note, I had the same problem and found that for some reason my input field was under the canvas and not the panel. Dragging it under the panel resolved the issue.
Your answer
Follow this Question
Related Questions
C# GUI Button 2 Answers
In-game text editor, native or NGUI 2 Answers
v4.6 Create GUI Elements Via Script? 1 Answer
Help with making a triggered gui message 1 Answer
Copy TextField to Clipboard via Button 0 Answers