Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by gaia2222 · Jul 02, 2020 at 10:25 AM · variableforeachdictionaryset

How can I set a dictionary value in foreach loop?

 Dictionary<BuffType, (BuffType type, int value, float time)> activeBuffList = new Dictionary<BuffType, (BuffType type, int value, float time)>();
 
 foreach (var activeBuff in activeBuffList)
                 {
                     (BuffType type, int value, float time) v = activeBuff.Value;
                     v.time -= Time.deltaTime;
                     activeBuffList[activeBuff.Key] = v;

 }

it pop up some error:collection was modified enumeration operation may not execute. c# dictionary in

 activeBuffList[activeBuff.Key] = v;

thankyou for your help~

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
1
Best Answer

Answer by Bunny83 · Jul 02, 2020 at 11:21 AM

You simply can't when you use unnamed tuples. Unnamed tuples are structs and therefore need to be assigned / copyied back into the dictionary. However The IEnumerator used to iterate through the dictionary will throw an exception whenever the content of the dictionary has changed.


You have several options:

  • replace your unnamed tuple with an actual class. That way you don't have to assign it back into the dictionary.

  • The other option is that you create a temp list / array of the content of your dictionary and iterate over that instead. The easiest way to do that for a dictionary is to use the Linq extension "ToList". So you add using System.Linq; at the top and you can do this


 foreach (var activeBuff in activeBuffList.ToList())

instead. So ToList will copy the content into a List which is used in the foreach loop. The rest of your code stays the same. This way the foreach loop doesn't care about any changes in the dictionary. However I would strongly recommend to use the first approach and exchanging the unnamed tuple with a class. That way you can simply change a value inside the class without the need of assigning it back

 foreach (var activeBuff in activeBuffList)
 {
     var v = activeBuff.Value;
     v.time -= Time.deltaTime;
 }

Of course as I mentioned this requires that your "value" is an actual class. Something like this:

 public class Buff
 {
     public BuffType type;
     public int value;
     public float time;
 }

Your dictionary would look like this:

 Dictionary<BuffType, Buff> activeBuffList = new Dictionary<BuffType, Buff>();

Of course we don't know how you actually populate your dictionary. But if should be obvious that you have to create those "Buff" instances when you want to add them to the dictionary.

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

136 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

Related Questions

How do I create a variable for each GameObject that I added to an array by using a loop? 2 Answers

C# Dictionary protection level when foreach-ing 1 Answer

Similar set of variables between classes... 2 Answers

Set variable in script from a different script... 3 Answers

Field Initializer Error 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