What happens when you, for example, call GetComponent<InputField>().text = "test"?
Do you get an error? Is your InputField visible and can you place text via the unity editor in the “Text” Gameobject that is a child of the InputField?
Thank you very much both of you. Now it works as a dream.
Yes it is an IntputField (TextMeshPro) from Unity 2022.(something)…
My only worry is how important this feature is, and still so hard to find an explanation
Working code for writing to input field:
using TMPro;
…
…
GameObject ifgo = GameObject.Find(“NameField”);
Debug.Log(ifgo);
TMP_InputField ifif = ifgo.GetComponent<TMP_InputField>();
Debug.Log(ifif);
ifif.text = this.playerName;
Thank you for this information I am using Unity in 2024 and could not get Text or TextFields to change text until I found this post, so thanks.
So now I can get a child of a canvas in unity and set its properties such as .text
For any interested or having troubles this might help you when it comes to updating a textfield in unity or changing text in unity ( assuming you added the TextField in the Unity UI ) or ( assuming you added the Text in the Unity UI ).
First, a C# utility function to get any child of a Canvas:
using UnityEngine;
public static Transform getCanvasChild(string canvas, string childName)
{
var rootCanvas = GameObject.Find(canvas);
var currentCanvas = rootCanvas.GetComponent<Canvas>();
if(currentCanvas is not null)
{
var myTransform = currentCanvas.GetComponentsInChildren<Transform>();
foreach (var target in myTransform)
{
if(target.name.Equals(childName))
{
return target;
}
}
}
return null;
}
C# Code using the above function:
using TMPro;
private void clearText(string canvas)
{
var inputTransform = Game.getCanvasChild(canvas, "InputField");
TMP_InputField currentInput = inputTransform.GetComponent<TMP_InputField>();
currentInput.text = "put text here";
}