For my project the default values are calculated base on an external output, these values can be changed using input fields in the new Unity UI. If the values are not changed a grey placeholder should appear after the calculation. I realy can not figure out how to change the placeholder text by script, not even find a solution anywhere. I tried this:
gameObject.GetComponent<InputField>().placeholder = uv.value;
The script is attached to the given Input Field game object. However to get the written value in the input field I use this line of code:
uv.value = gameObject.GetComponent<InputField>().text;
It works fine. Did I miss something? Some help would be appreciated, to write here is my last resort. Thank you forward!
To find the GUIText object, you can also use GameObject.Find, like so: For effieciency, do this in the Start () function and save it to a private GUIText variable. As of Unity 2.0, you can also use the GUI.Label function.
As of Unity 2.0, you can also use the GUI.Label function. First put using UnityEngine.UI; then do the following and attach this script to UI TEXT Control.
// Create the Text GameObject . GameObject textGO = new GameObject (); textGO.transform.parent = canvasGO.transform; textGO.AddComponent< Text > (); // Provide Text position and size using RectTransform .
This is when there is focus on it. A placeholder graphic can be used to show subtle hints or make it more obvious that the control is an InputField. If a Text component is used as the placeholder it is recommended to make the placeholder text look different from the real text of the InputField so they are not easily confused.
Placeholder is just a Text component. You can change it's text by:
gameObject.GetComponent<InputField>().placeholder.GetComponent<Text>().text = "Something";
Note that GetComponent<InputField>().placeholder
is a Graphic component, which is not the droid you are looking for :)
You could also downcast the placeholder
object since Text inherits from Graphic class.
Something like this will also work.
Graphic graphic = gameObject.GetComponent<InputField>().placeholder;
((Text)graphic).text = "Hello";
This works for me but remember to assign the placeholder gameobject to the Input Field Settings->Placeholder otherwise your get a null error.
TextMeshProUGUI placeholder = (TextMeshProUGUI)userNameInput.placeholder;
placeholder.text = "Hello";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With