I’ve bumped into one of those weirdo C#/Unity things that TOTALLY DOES NOT MAKE SENSE.
Here’s what’s happening… I have a List<> names myAvailableLanguages and want to allow a user to pick a (new) language from a dropdown UI object. Aside from that dropdown UI object, I have a 2nd one that allows the user to pick a language group (like Germanic, Slavic or others - the list of languages is HUGE). There are also some bools connected to myAvailableLanguages and I got those toggles to toggle UI objects. This works perfectly and no problems there.
Now I want to use the toggle UI objects where the selected language plays a role if it’s intractable/toggled or not. For this I’ve written the following code:
Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()==myDropdownLanguages.options[myDropdownLanguages.value].text.ToString())].Language);
I won’t go into too much detail on this line, but in theory it’s 100% okay. Yet the line gives the error of this thread’s topic. When I break down the line into several elements, they all give the right response.
When I use:
Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()=="Japanese")].Language+" "+myDropdownLanguages.options[myDropdownLanguages.value].text.ToString());
and select Japanese from the dropdown UI object, the line debugs as “Japanese Japanese” as it should.
Also look at the above line and the 1st one. Both use identical statements, where in the 1st one I substituted “Japanese” for the 2nd part of the line above here.
When I use:
string tempstring=myDropdownLanguages.options[myDropdownLanguages.value].text.ToString();
Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()==tempstring)].Language);
I get the correct response of the current language selected from the dropdown UI object and once again, these 2 lines are basically the same as the one I’ve listed 1st.
Though it’s clear for me to use the last line examples to get the code I want to work as it should, I just can’t stop to wonder about the WHY the 1st line with an actual correct statement gives this ArgumentException error…