Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ClioStark · Apr 28, 2020 at 11:05 AM · arrayslistsfor-loopswitch-case

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ClioStark · Apr 28, 2020 at 11:14 AM 0
Share

The switch statement only executes at the first iteration by the way and never runs thereafter if that makes a difference.

avatar image Hellium · Apr 28, 2020 at 11:15 AM 0
Share

You are creating a new list every iteration. Shouldn't you create it once before entering the for loop?

avatar image ClioStark Hellium · Apr 28, 2020 at 11:25 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges