How to change placeholder text in Unity UI using script?

For my project, the default values ​​are calculated on the external output, these values ​​can be changed using the input fields in the new Unity user interface. If the values ​​do not change, a gray placeholder should appear after the calculation. I really can't figure out how to change the placeholder text to a script without even finding a solution anywhere. I tried this:

gameObject.GetComponent<InputField>().placeholder = uv.value;

The script is attached to this game object in the input field. However, to get the written value in the input field, I use this line of code:

uv.value = gameObject.GetComponent<InputField>().text;

It works great. Did I miss something? Some help would be appreciated to write here, this is my last resort. Thanks you!

+6
source share
2 answers

A placeholder is just a text component. You can change its text:

gameObject.GetComponent<InputField>().placeholder.GetComponent<Text>().text = "Something";

Please note that it GetComponent<InputField>().placeholderis a graphical component that is not the droid you are looking for :)

+11
source

You can also reduce the value of an object placeholder, since Text inherits from the Graphic class. It will work in approximately the same way.

Graphic graphic = gameObject.GetComponent<InputField>().placeholder;
((Text)graphic).text = "Hello";
0
source

All Articles