Let say I have this list and currently there are three animals inside the list. These names below are displayed on the text field in the scene.




If there is a new animal is added to the list, for example a 'dog', the code will add 'dog' below the 'Goat' inside the text field above. Now I have a problem when the same animal is added to the list. I don't know how to actually display the updated text field if the same type of animal is added to the list and will not messing up the text field or duplicating the same name inside the text field. Please help.

This is the code

foreach (var animalnames in newComp)
            if((componentName.text.Length !=0)&&(animal.transform.name != animalnames.name))
                componentName.text = oldName+('\n')+('\n')+('\n')+animal.transform.name;
            if((componentName.text.Length !=0)&&(animal.transform.name == animalnames.name))
                animalnames.name = animal.transform.name;
                //componentName.text = ('\n')+animalnames.name;
        oldName = componentName.text;
  • \$\begingroup\$ I have 2 question. Is newComp a list? And what exactly do you want to do, like you don't want to show duplicate text or you wanna show duplicate text but at the bottom? \$\endgroup\$ Commented Jun 7, 2017 at 10:35
  • \$\begingroup\$ Yes, newComp is a list, and I want to make some sort of list of animals, and yes, I don't want to show duplicate text. \$\endgroup\$ Commented Jun 8, 2017 at 1:25
  • \$\begingroup\$ I see that @Xesenix solution worked for you. But I'll leave my suggestion as well. You can use LINQ here. To avoid saving duplicates, just use Contains method to check the text you want to add already exist or not. \$\endgroup\$ Commented Jun 8, 2017 at 4:57

1 Answer 1


You should probably use StringBuilder for creating text field content something like this:

// somewhere in component constructor
this.sb = new StringBuilder()
this.animalNamesList = new List<string>()

// when adding new name
if (!this.animalNamesList.Contains(name)) {

// and in update text field content
foreach (string animalName in this.animalNamesList) {
this.textField.text = this.sb.ToString()

Check more about String builder:


and List:


  • \$\begingroup\$ Thanks for the reply, the code is working except when I try to clear the string with this this.sb.clear(), it doesn't exist. Am I missing something? \$\endgroup\$ Commented Jun 7, 2017 at 9:51
  • \$\begingroup\$ did you create it in class constructor do you have 'this.sb = new StringBuilder()' somewhere? \$\endgroup\$
    – Xesenix
    Commented Jun 7, 2017 at 10:48
  • 1
    \$\begingroup\$ If you are using C# 4.0 or higher compiler, you can use string.Join, e.g. text = string.Join("\n\n", newComp.Select(an => an.name)), chaining select with Distinct if you want just distinct values. \$\endgroup\$
    – wondra
    Commented Jun 7, 2017 at 12:31
  • \$\begingroup\$ @Xesenix I use 'this.sb = new StringBuilder()' when I'm initializing it, but it's solved now. I just use 'stringBuilder.Length = 0' to clear the list, but I still don't know how not to make the text field not displaying duplicate texts. \$\endgroup\$ Commented Jun 8, 2017 at 1:30
  • \$\begingroup\$ You guys are amazing! I use @Xesenix code and I use Distinct like @wondra suggested and it worked now. Thanks again :) \$\endgroup\$ Commented Jun 8, 2017 at 1:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .