Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change placeholder text in Unity UI using script?

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!

like image 867
Erik Putz Avatar asked Apr 16 '15 17:04

Erik Putz


People also ask

How to find the guitext object in Unity?

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.

How to add a label to a text control in Unity?

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.

How to create a text GameObject in Unity?

// Create the Text GameObject . GameObject textGO = new GameObject (); textGO.transform.parent = canvasGO.transform; textGO.AddComponent< Text > (); // Provide Text position and size using RectTransform .

When should I use a placeholder graphic?

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.


3 Answers

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 :)

like image 143
Can Baycay Avatar answered Sep 29 '22 09:09

Can Baycay


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";
like image 31
Tushar Chawla Avatar answered Sep 29 '22 09:09

Tushar Chawla


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

like image 36
Mark Day Avatar answered Sep 29 '22 11:09

Mark Day