- Home /
How to add text values to a list
Hello people. Im triying to make a list of int/text values, taking them from a unique gameobject. and I need prevent values from repeating in the list. PD: the value is updated in runtime.
there is my attemp:
public List<Text> ValuesN = new List<Text>();
public Text Values;
public Text Result;
public void addToList()
{
if (ValuesN.Contains(Values))
{
Debug.Log("Already exist in list");
}
else
{
ValuesN.Add(Values);
}
var MergeValues = string.Join("\n", ValuesN.Select(obj => obj.GetComponent<Text>().text));
Result.text = MergeValues;
}
this simply doesnt works, only show the last value in "Result". some guidance?,please. Thanks.
image of the basic idea:
SOLUTION:
public List<string> ValuesText = new List<string>();
public Text Values;
public Text Result;
public void addToListV4()
{
ValuesText.Add(Values.text);
foreach(string s in ValuesText)
{
Text clonedText = Values;
}
var MergeValues = string.Join("\n", ValuesText);
Result.text = MergeValues;
}
//this function prevent repeated values in the list
public void addToListV5()
{
if (ValuesText.Contains(Values.text))
{
Debug.Log("Already exist in list");
}
else
{
ValuesText.Add(Values.text);
}
foreach (string s in ValuesText)
{
Text clonedText = Values;
}
var MergeValues = string.Join("\n", ValuesText);
Result.text = MergeValues;
}
Answer by elfasito · Oct 03, 2020 at 11:08 AM
Solution:
public List<string> ValuesText = new List<string>();
public Text Values;
public Text Result;
public void addToListV4()
{
ValuesText.Add(Values.text);
foreach(string s in ValuesText)
{
Text clonedText = Values;
}
var MergeValues = string.Join("\n", ValuesText);
Result.text = MergeValues;
}
//this function prevent repeated values in the list
public void addToListV5()
{
if (ValuesText.Contains(Values.text))
{
Debug.Log("Already exist in list");
}
else
{
ValuesText.Add(Values.text);
}
foreach (string s in ValuesText)
{
Text clonedText = Values;
}
Answer by enestelli · Oct 02, 2020 at 09:12 PM
Hello @elfasito I'm not sure, but have you tried expanding the text field? Maybe, because the text field is small, the rest of the text has not appeared? Because I can't see a problem in the code.
Hello @enestelli the text field is ok, the problem is when I try to add new values to the list. I get the Debug.Log("Already exist in list"); ( so the list only have one element). idk why I get that, the int values I try to add are differents. I see some examples use foreach for the iteration, but im not sure how implement it.
Can you send me the code for the part you're trying to add to the list? So where do you assign this text variable named Values? Not the ValuesN.
Hello enestelli I finally resolved myself, I added my actual solution. thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613012955im_/https://answers.unity.com/themes/thub/images/avi.jpg)