How to change the text of a button with a name from a list without the name repeating?

I’ve been attempting to change the text of each button I have in my scene with that of pre-existing GameObjects that carry a specific tag that designate locations where the user can go to.

    public Button[] bTons;
    public List<GameObject> sTor = new List<GameObject>();

I’ve created the list and array for the gameobjects / buttons I would be using and I successfully added them to their respective list. However, when it comes to changing the text of the Buttons I am unable to figure out how to tell each button that they must take one element from the sTor list and change their text to the name of the element they picked. That means that once one of the elements in the list has been take by one button the next button should not take that element as well but go to the next.

I’ve attempted to do this with a foreach loop mixed with another foreach loop and had no success.

        foreach (Button button in bTons)
        {
            var txt = button.gameObject.GetComponentInChildren<Text>();
            foreach (var list in sTor)
            {
                txt.text = list.name;
            }
        }

I’ve also attempted to go through the list with a for and had the same result: All buttons within the array had their text changed to just the first element in the list, they had completely ignored the second, third, fourth and so on element.

Any help would be greatly appreciated. Thank you.

You just needed to use a plain old for loop.

for (int i = 0; i < buttons.Length; i++) 
{
    Text buttonTextComponent = buttons*.GetComponentInChildren<Text>();*

//^the .gameObject was unnecessary

buttonTextComponent.text = locationObjects*.name;*
//and there we go! button text changed to the proper name!

}
The problem with your foreach loop was how it didn’t consider the positions of the objects in the containers (as foreach loops tend to do). Though if you still wanted to use a foreach loop, you’d need to use an outside counter variable, like so:
int index = 0;
foreach (Button button in buttons)
{
Text buttonTextComponent = buttons[index].GetComponentInChildren();

buttonTextComponent.text = locationObjects[index].name;
index++; //make sure to update the counter!
}
Also note how I used the name “buttons” for bTons, and “locationObjects” for sTor. I can respect making variable names short, but it’s best when the shortness doesn’t make the names too vague.
Hope this helps! :slight_smile:

After fidling around more I somehow found a solution to my problem, though I have the feeling that it will bite me in the end in performance.

        for (int u = 0; u < bTons.Length; u++)
        {
            for (int i = 0; i < sTor.Count; i++)
            {
                var txt = bTons.GetComponentInChildren<Text>();

switch (u)
{
case 0:
txt.text = sTor[0].name;
break;
case 1:
txt.text = sTor[1].name;
break;
}
}
}
I’m sure people will be able to tell that if I want to add more buttons (lets say 10) I’d have to hardcode all of them from the list to get the desired effect.
If anyone stumbles upon this and can provide a much clearner solution please do, I’d be more than thankful, but for the moment it seems like I’ve found a solution on my own.