• 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?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

C# List index confusion using Mathf.Clamp 1 Answer

How do you play an array of audio clips in sequence with dialogue system on mouse click? 0 Answers

How do I create a list of GameObjects & Integers? 1 Answer

How to sort List of Lists by a certain Value (C#) 1 Answer

Why is my List adding to same array object again and again in update? 1 Answer


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