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 Aldwoni_legacy · May 17, 2011 at 08:31 AM · collisionlistexceptionremove

InvalidOperationException: The list was modified.

InvalidOperationException: The list was modified. Boo.Lang.List`1+c__Iterator6[UnityEngine.GameObject].MoveNext () Item.OnCollisionExit (UnityEngine.Collision collision) (at Assets/Item.js:73)

I simply want a list with item I currently collide with, so when I stop colliding they must be removed from the list:

EDIT: I changed my code but now it gives an index out of range exception

 function OnCollisionExit(collision : Collision) {
     for(var i = tegenstanders.Count; i> 0; i--)
     {
         item = tegenstanders[i];
         if(item.name == collision.gameObject.name)
         {
             tegenstanders.Remove(tegenstanders[i]);
         }
     }
 }
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
9
Best Answer

Answer by CHPedersen · May 17, 2011 at 08:45 AM

This happens because you're removing items from the list at the same time as you are iterating through it. It's easier to explain if you convert the list to a non-enumerated for-loop instead:

 List<int> example = new List<int>();
 for (int i = 0; i &lt; example.Count; i++)
 {
     if (something)
         example.Remove(example[i]); // Not safe to do
 
 }

If you remove an item from the list while you're going through it, the for-loop's impression of the list's Count-property is no longer reliable, since the amount of elements in the list changed during execution of the loop. In addition, the enumeration is now messed up; example[i] now points to another item in the list, because the Remove-command has shifted all of the elements with a higher index down to keep them sequential in memory, so you would effectively skip an item if you kept iterating with it. That's why the enumerated version of the loop doesn't enjoy it very much when you modify the List in it.

To get around this issue, use a standard for-loop and iterate through the list backwards instead:

List<int> example = new List<int>();
for (int i = example.Count-1; i >= 0; i--)
{
    if (something)
        example.Remove(example[i]); // Safe to do
}

This is not a problem, because you're stopping iteration at 0, not at List.Count, and all of your indexes still point to the right elements just fine, because the elements get shifted down on Remove, and you've already iterated through those.

Comment
Add comment · Show 6 · 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 Aldwoni_legacy · May 17, 2011 at 08:52 AM 0
Share

Can you tell me what the "Boo" part of the error means?

avatar image CHPedersen · May 17, 2011 at 09:18 AM 0
Share

Well, I'm not a Boo programmer. But it looks like the error occurred when the Boo-script tried to call "$$anonymous$$oveNext", which suggests that it found out the list had been changed when it attempted to read the next element, and then it simply reported just that: "The list was modified".

avatar image Aldwoni_legacy · May 19, 2011 at 08:09 AM 0
Share

The problem is that I didn't porgrammed in Boo but in Unityscript(js)

avatar image T_Lavell · Feb 14, 2015 at 01:37 AM 0
Share

What a tremendously helpful answer!

avatar image guironm · Jun 21, 2015 at 01:25 PM 0
Share

indeed the reverse for loop is also interesting..hope it works

Show more comments

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

1 Person is following this question.

avatar image

Related Questions

Removing items from a list while iterating through it? 3 Answers

A node in a childnode? 1 Answer

Is there a way to organize tracking of collisions? 0 Answers

can add to a list but not remove 1 Answer

How to remove project from the Project Wizard 6 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