I’m trying to populate dropdown menus from lists of GameObjects, and then select those game objects by their index.
So, the population of the dropdowns works just fine. However, the selection does not. I wind up with, from the first list, many, many gameobject.name instances in the console. And from others, an exception message: Argument out of Range exception. Index was out of range. Must be non-negative and less than the size of the collection
.
Here is the relevant snipped of my code:
public List list_TacticalTargetTypeSelector = new List() { "Allied Starships", "Hostile Starships", "Allied Starbases", "Hostile Starbases", "Asteroids" };
public string tacTargetType;
public TMP_Dropdown dropDown_TMP_TacticalTargetSelector;
public void addGamePiecesToTacticalTargetSelectorDropDownMenu()
{
if (tacTargetType == "Allied Starships")
{
dropDown_TMP_TacticalTargetSelector.ClearOptions();
dropDown_TMP_TacticalTargetSelector.AddOptions(listManagerGlobal.list_AlliedStarships.ConvertAll(alliedStarships => alliedStarships.name));
}
else if (tacTargetType == "Hostile Starships")
{
dropDown_TMP_TacticalTargetSelector.ClearOptions();
dropDown_TMP_TacticalTargetSelector.AddOptions(listManagerGlobal.list_HostileStarships.ConvertAll(hostileStarships => hostileStarships.name));
}
[etc., etc...]
}
public void selectGamePieceFromTacticalTargetSelectorDropdown()
{
if (tacTargetType == "Allied Starships")
{
dropDown_TMP_TacticalTargetSelector.onValueChanged.AddListener(index => Debug.Log(listManagerGlobal.list_AlliedStarships[index]));
}
else if (tacTargetType == "Hostile Starships")
{
dropDown_TMP_TacticalTargetSelector.onValueChanged.AddListener(index => Debug.Log(listManagerGlobal.list_HostileStarships[index]));
}
[etc., etc....]
}
addGamePiecesToTacticalTargetSelectorDropDownMenu()
is in the Update function.
and
selectGamePieceFromTacticalTargetSelectorDropdown()
is attached to the dropdown in the on value changed(32int) option.
I’m pretty new to Unity and C#, so maybe I’m missing something obvious.
Thanks again for your help, and if you have any answers regarding this, I’d love to hear them.