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 Goonpac · Mar 25, 2018 at 08:48 PM · ifforeachefficiencycycleassign-variable

Efficiency problem during foreach cycle

I have this code, which I think is not very efficient since it checks for the current Language for every item in the list:

 foreach (InfoSpecies c in list.Especies) //list has 37 items
 {
     if (Localization.Instance.CurrentLanguage == SystemLanguage.Portuguese)
     {
         nomeComum = c.NomeComum;
     }
     else
     {
         nomeComum = c.CommonName;
     }
     //do something with nomeComum
 }

I'm trying to change the code in order to perform the test only once, just before running the foreach cycle. I think it has to be something like this:

 if (Localization.Instance.CurrentLanguage == SystemLanguage.Portuguese)
 {
     nomeComum = c.NomeComum;
 }
 else
 {
     nomeComum = c.CommonName;
 }
 
 foreach (InfoSpecies c in list.Especies)
 {
     //do something with nomeComum
 }

But this is obviously not working, since c.NomeComum will only exist during the foreach cycle. Is there a way to pass the apropriate assignment after the if cycle has run, in order for the right item to be selected from the list? Can someone help me? Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by exzizt · Mar 25, 2018 at 10:01 PM

If c.NomeComum is the same for each element in the list, you can do:

 nomeComum = list.Especies[0]; // This gets the first element from the list (there must be one!).
 
 // Now go through your foreach.
Comment
Add comment · Show 2 · 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
avatar image Goonpac · Mar 25, 2018 at 11:11 PM 0
Share

Thank you for your answer exzizt, but unfortunately, nomeComum is always different.

The purpose of the foreach cycle is to populate a scrollable list that receives its elements from a JSON file containing a list of marine species, depicted by their scientific name and their English and Portuguese equivalents. And the reason for the if cycle is to figure it out if the system language is in English or Portuguese, thus presenting the species name in the appropriate language (i.e., c.CommonName or c.NomeComum from the JSON).

Everything is fine and working, but I think that the way I have my code, it's not the most efficient way to do this. I mean there are 37 species in the list, and the current code checks for the system language each time a species is added to the scrollable list.

I could simply put the foreach cycle inside each of the if else statements, and then changing the nomeComum assignment, thus solving my problem. But the foreach cycle is much more extensive than the one shown here, and since the only thing changing would be the nomeComum assignment, almost 98% of the foreach code would be duplicate in each of the the if else statements. I think there is no need for this redundancy, there must be other way...

avatar image exzizt Goonpac · Mar 27, 2018 at 03:11 AM 0
Share

If it's always different for each item in the loop, then you have to check it for each item in the loop.

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

128 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

Related Questions

Dialogue Skipping Issues? 0 Answers

Spawning multiple gameobjects with random scales 2 Answers

Iterating through a custom keycode enum for input detection 1 Answer

How to flip 2D object, mine isnt working nor any others i can find. 1 Answer

Add changeable conditions in the editor, similar to unityEvent? 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