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
1
Question by kbabilinski · Jul 25, 2012 at 09:16 PM · c#listforeachguilayout

"enumeration operation may not execute" Problem with C# list

So I am making buttons with this code

void gcListItem(Texture strItemName) { GUILayout.BeginHorizontal(); if(GUILayout.Button((strItemName), GUILayout.Height(100))){ Debug.Log(strItemName.name); Buttons.Remove(strItemName); } GUILayout.EndHorizontal(); } void gcListBox() { GUILayout.BeginVertical(GUI.skin.GetStyle("Box")); GUILayout.BeginArea (new Rect (40,230,400,300)); scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(250), GUILayout.Height(250)); foreach(Texture _texture in Buttons){ gcListItem(_texture); }

The problem is that when I click on the button I get this error : [InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List`1+Enumerator[UnityEngine.Texture].VerifyState () System.Collections.Generic.List`1+Enumerator[UnityEngine.Texture].MoveNext ()]

The code that causes this error is Buttons.Remove(strItemName);

The question is how do I fix it please 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
22
Best Answer

Answer by Brian Stone · Jul 25, 2012 at 10:13 PM

This is a design feature of .NET list enumerators, you can't modify the underlying object referenced by an enumerator. Foreach loops iterate through a list via enumerators, specifically the IEnumerator interface.

When using the List class, you can avoid this situation two ways...

1) By iterating through the collection via array indexes, thereby avoiding enumeration entirely.

 for(int i=0; i<Buttons.Count; i++)
 {
    Texture _texture = Buttons[i];
    gcListItem(_texture);
 }

...OR...

2) By copying the collection and iterating through the copy.

 foreach(Texture _texture in Buttons.ToArray())
 {
    gcListItem(_texture);
 }

This works because the Button list isn't being enumerated in this case, the new array is, so you are free to modify the Button list any way you want. However, copying the Button list is obviously an expensive operation, you don't want to use this technique if the list is large or if this code called very frequently.

Hope that answers the question. :)

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 Most_Incredible · May 15, 2015 at 06:30 AM 0
Share

Thank you $$anonymous$$, it worked for me.

avatar image XhryZed · Oct 25, 2021 at 09:52 AM 0
Share

tried figuring this out for 2 hrs before co$$anonymous$$g here. still helpful in 2021 :D

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

7 People are following this question.

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

Related Questions

C# Problem Creating List of Texture2Ds 0 Answers

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

foreach a array inside a generic list 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