- Home /
Display strings from a list in a text box
I think I am just missing one simple thing. I have an inputfield, button and two text boxes. The first text box("output") is pulling its text from the inputfield. I want the second textbox("englishWords") to pull its text from the list that is being made. sender() is what the button calls.
Here is my code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class butt : MonoBehaviour {
public GameObject input;
public GameObject output;
public GameObject englishWords;
public List<string> words = new List<string>();
public void sender()
{
output.GetComponent<Text> ().text = input.GetComponent<InputField> ().text;
input.GetComponent<InputField> ().text = "";
words.Add (input.GetComponent<InputField> ().text);
for (int i = 0; i < words.Count; i++)
{
englishWords.GetComponent<Text> ().text = (words[i]);
}
}
it works :) but i have to display the text in new line all time when i iterate..Like this
Line 1 : words[0] Line 2 : words[1] Line 3 : words[2]....
what should i have to do...now
I got that too..Tested code
englishWords.GetComponent ().text +="\n"+ words[i];
it works..
Answer by DiegoSLTS · Mar 22, 2015 at 02:36 PM
input.GetComponent<InputField> ().text = "";
words.Add (input.GetComponent<InputField> ().text);
You're setting the InputField's text to an empty string and then adding the InputField's text (the empty string) to the words list, you should add the new word to the list first.
for (int i = 0; i < words.Count; i++)
{
englishWords.GetComponent<Text> ().text = (words[i]);
}
You're setting the text value for each word in the list, but you're overwriting the previous value each time. You have to append the words instead, something like this:
englishWords.GetComponent<Text> ().text = "";
for (int i = 0; i < words.Count; i++)
{
englishWords.GetComponent<Text> ().text += words[i];
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613190814im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Layout group children adjust to content 1 Answer
Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers
Prevent focus from leaving inputfield 0 Answers
How can I change the phrase "I am sure you are right." to "You are sure I am right."? 0 Answers