I made a list containing all the buttons in the UISelectionCanvas and would like to change each image as it is being selected. The problem is once the selection selects a new type of unit it will change all the buttons sprites in the list to the new selection instead of just changing the one selected.
for a example imagine I have 10 units 9 peasants and 1 warrior
as each peasant is selected the sprite adds the peasant icon to the button. if 8 peasants are selected and then the 9th selection is a warrior it changes all 9 buttons to a warrior icon then if I selects the 10th peasant it changes them all back to peasant.
for (int i = 0; i < buttons.Count; i++)
{
for (int c = 0; c < SelectedUnits.Count; c++)
{
if (i <= c)
{
buttons[i].SetActive(true);
foreach (var unit in SelectedUnits)
{
if(unit.tag == "Peasant")
{
buttons[i].transform.GetComponent<Image>().sprite = peasant as Sprite;
}
else if(unit.tag == "CityGuard")
{
buttons[i].transform.GetComponent<Image>().sprite = warrior as Sprite;
}
}
}
else
{
buttons[i].gameObject.SetActive(false);
}
}
}