- Home /
Change InputField text on ButtonClick
So , I have 4 Buttons and 2 text fields. And I want to show user different values in this InputFields. (Click first Button - "1" in first inputfield and "2" in second inputfield etc)
(Inputfields in my program it's just GameObject -> UI -> InputField)
So , my problem is that I don't know how to change text in this fields.
I'm adding new function OnClick to each button , but how can I change text in this inputfields?
p.s. Sorry for my bad english.
Answer by misticone00 · Jan 05, 2015 at 11:28 PM
Hello, If you look carefully you will notice that inside the inputField element in your Hierarchy view as a child name Text.
Ok, so in order to change the InputField text you need to get access to the script that is responsable for showing determinated text in the InputField.
You can Change it by doing something as simple and clean as this:
void ChangeInputFieldText(string message){
Text textscript = GameObject.Find ("InputField").GetComponentInChildren<Text>(); // This will get the script responsable for editing text
if(textscript == null){Debug.LogError("Script not found");return;}
textscript.text = message; // This will change the text inside it
}
void Start(){
ChangeInputFieldText("Hello Dude!!");} //Time to call our super awesome function!!
Hope I could help,
Happy New Year
Your answer
Follow this Question
Related Questions
Custom Android Textfield and Button 0 Answers
Typing text and close the box 0 Answers
Problem with GUI.Button and other GUI items 1 Answer
textfield and button submit 1 Answer