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
2
Question by RaulG · Jun 11, 2013 at 06:38 PM · listloopforeach

InvalidOperationException Collection was modified;

Getting this error here:

 tList.Add(PlatformOBJChild);
 foreach (Transform t in tList) // <-- Error here
 {
 if (t == null)
 {                         tList.Add(PlatformOBJChild);
 }
 }
Comment
Add comment · Show 2
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 InfiniBuzz · Jun 11, 2013 at 07:13 PM 0
Share

is your tList initiated? the foreach loop goes through all (existing) elements in a list so why do you check if t is null?

What type is tList?

avatar image RaulG · Jun 11, 2013 at 07:21 PM 0
Share

tList is a Transform list, and it is initiated. I think... It should be!

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by TonyLi · Jun 11, 2013 at 07:31 PM

You shouldn't modify the contents of collections while you're iterating on them.

You can use a temporary list to do this:

 List<Transform> itemsToAdd = new List<Transform>();
 
 foreach (Transform t in tList) {
     if (t != null) {
         foreach (Transform child in t) {
             itemsToAdd.Add(child);
         }
     }
 }
 
 foreach (Transform item in itemsToAdd) {
     tList.Add(item);
 }

Note that the first foreach loop will only add children one level deep. You could use a recursive function (or something similar) to recursively add all children and their children.

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 tsmitro · Jun 11, 2013 at 07:39 PM 0
Share

Example of what I just explained... +1 for not being lazy like me.

avatar image RaulG · Jun 11, 2013 at 08:21 PM 1
Share

FANTASTIC! That worked!

Now I just need it to stop giving the $$anonymous$$issing(Transform) in the inspector.

EDIT: Fixed! Unity you sexy bitch! if (tList.Contains(null)) { tList.Remove(null); }

Did not know you can check for a null in a list, remove it, and then it will automatically add the correct object.

avatar image
0

Answer by tsmitro · Jun 11, 2013 at 07:28 PM

You are receiving that error because you are modifying your collection while iterating over it... that error is thrown on the second iteration because you have added another element. Read this if you want a more in depth explanation. Bottom line: do not add to, remove from or change values of any collection while you are looping through it.

Comment
Add comment · Show 3 · 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 RaulG · Jun 11, 2013 at 07:47 PM 0
Share

Is there a... Unity version? Like what TonyLi posted?

avatar image tsmitro · Jun 11, 2013 at 08:16 PM 0
Share

Take his verbatim, he made it easy for you... this isn't related to Unity at all, it deals with Collections in the .NET Framework.

avatar image RaulG · Jun 11, 2013 at 08:23 PM 0
Share

You mean! I'm not working in Unity! $$anonymous$$y whole life is a lie!

Thanks anyway :P.

avatar image
0

Answer by florinel2102 · Dec 07, 2020 at 09:01 PM

If you wanna to delete an element (or more) just iterate with for/while and increment index only when the item is not deleted and pay attention to if you save count (n = exampleList.Count) in a variable to decrement when an element is deleted .

     for(int i = 0; i<exampleList.Count;)
     {
         if(exampleList[i] == null)
         {
            exampleList.Remove(exampleList.[i]);
             continue;
         }
         ++i;
     }


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

18 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

Related Questions

foreach loop problem 1 Answer

How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer

A node in a childnode? 1 Answer

Guarantee loop order of child objects. 1 Answer

foreach Gameobject in array problems 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