Switch statement nested in a for loop runs only in one iteration of the loop
Good day everyone. I have the following code and the goal here is to "cast" the strings in the switch statement into an enum and save the enums in a separate list.
IntelligencesSeparated = new List<Intelligence>();
for (int i = 0; i < IntelligencesSeparatedTemp.Length; i++)
{
string intelligenceName = IntelligencesSeparatedTemp[i];
Debug.Log(intelligenceName);
switch (intelligenceName)
{
case "Visual-Spatial":
IntelligencesSeparated.Add(Intelligence.VisualSpatial);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.VisualSpatial);
break;
case "Musical":
IntelligencesSeparated.Add(Intelligence.Musical);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.Musical);
break;
case "Logical-Mathematical":
IntelligencesSeparated.Add(Intelligence.LogicalMathematical);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.LogicalMathematical);
break;
case "Linguistic-Verbal":
IntelligencesSeparated.Add(Intelligence.LinguisticVerbal);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.LinguisticVerbal);
break;
case "Naturalistic":
IntelligencesSeparated.Add(Intelligence.Naturalistic);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.Naturalistic);
break;
case "Interpersonal":
IntelligencesSeparated.Add(Intelligence.Interpersonal);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.Interpersonal);
break;
case "Intrapersonal":
IntelligencesSeparated.Add(Intelligence.Intrapersonal);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.Intrapersonal);
break;
case "Bodily-Kinesthetic":
IntelligencesSeparated.Add(Intelligence.BodilyKinesthetic);
Debug.Log(intelligenceName + "cast as enum " + Intelligence.BodilyKinesthetic);
break;
}
}
The IntelligencesSeparatedTemp array is an array of strings separated by commas which I already split at the commas beforehand so that part is already working. I'm doing the above code for multiple arrays but the problem is that it only executes the switch statement once for the entire duration of the for loop so my IntelligencesSeparated array only has one item every time the loop runs instead of having the same number of items as IntelligencesSeparatedTemp.
The switch statement only executes at the first iteration by the way and never runs thereafter if that makes a difference.
You are creating a new list every iteration. Shouldn't you create it once before entering the for
loop?
Thanks for spotting that I changed that part in my code now. I tried replicating this with if statements ins$$anonymous$$d of switch case with the exact same result. Separating the switch case to its own method apparently also doesn't work.
Answer by ClioStark · Apr 29, 2020 at 08:53 AM
Found the answer. I just had to trim the leading and trailing spaces in each item in the IntelligencesSeparatedTemp array with string.trim() and then run the switch statement.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
For Loop looping out of Loop Condition 1 Answer
Endless Runner with new Biome each time 0 Answers
instantiate from two seperate values in for loop 1 Answer
Increase List of Spawns Dynamically 1 Answer
How to load specific data from XMLArray 0 Answers