- Home /
TextField text component not updating
Hi Unity community!
I am using the UI InputField gameobject and am able to retrieve its string value easily via the Text.text component.
However I can't seem to update the component's string value using the same method (I want the error report to be assigned to the Input Field's displayed text). The function reports the Debug error fine so I assume it is the way I am assigning the new string.
Can someone explain to me why this is incorrect? I have searched all over to no avail.
Many thank in advance!
Ryan
public function ObjNameEd(){ //function for editing the name of gameobject via GUI
var curObj : GameObject = GameObject.FindGameObjectWithTag("Current Model");
var userField = GameObject.Find("ObjectNameFieldText").GetComponent(Text);
if(curObj != null){
if(userField.text.Length > 5 ){ //Correct Input
var newObjName : String = userField.text;
curObj.name = newObjName;
} else if (userField.text.Length < 5){ //Incorrect Input: Error
var errorRep : String = "Must be 5+ chars";
userField.text = errorRep;
Debug.Log("Must be 5+ chars");
}
} else {
Debug.Log("No model uploaded");
}
}
I don't think you can change the input text while the user is inputting the text....
I suggest you use another Text game object to display "$$anonymous$$ust be 5+ chars" ins$$anonymous$$d of trying to override the user's input.
Yes I think this is the main issue I am experiencing, thanks!
Answer by NeverHopeless · Aug 05, 2015 at 02:07 PM
Perhaps you should display error like that:
As a short procedure, you can duplicate the Text
gameobject inside InputField
gameObject, and name it as Error
and set error like:
GameObject.Find("ObjectNameFieldText").transform.Find("Error").GetComponent<UnityEngine.UI.Text>().text = "Error occured";
Reference image taken from here.
@NeverHopeless this is probably the most appropriate solution, thanks!
Answer by Heli · Aug 05, 2015 at 02:38 PM
Input field is only for capturing user inputs.You can not assign values. If you want to display something, i suggest you use a text field.
one possible error I can see in this code is in line 15. It should be errorRep=userField.text; .
Your answer
Follow this Question
Related Questions
RPG Text 2D on Object Interaction Javascript 0 Answers
I'm trying to display my ammo; not working 2 Answers
Line break UI Text field 1 Answer
Variable won't appear on UI text (JS) 0 Answers
sending a variable to a text object 1 Answer