Search code examples
c#unity-game-enginecomponentsplaceholder

How to set the 'placeholder' property in 'Input Field' component via c# script? (Image attached)


This is 'Input Field' component capture image.

Blue box is 'Placeholder' without linked. I want to set this value with my placeholder object

my placeholder

I mean 'my placeholder object' below.

In unity editor, I can set this using dragging 'my placeholder object' to blue box.

My question point is that how to get this via script.

enter image description here

InputField myInputField = myGameObject.AddComponent<InputField>();
myInputField.GetComponent<InputField>().placeholder = placeH01; 

<< not working, type mismatch error with GameObject and UI.Graphic


Solution

  • I don't see what placeH01 is in your code but it sounds like it is a GameObject and you probably rather would want

    // The GetComponent<InputField>() is quite redundant btw 
    // myInputField already IS an InputField
    myInputField.placeholder = placeH01.GetComponent<UI.Graphic>();