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

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; .

Perhaps you should display error like that:

alt text

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.