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 gtaharaedmonds · Nov 04, 2016 at 01:08 AM · listsclassescoroutinesdebugging

Collection was modified; enumeration operation may not compute

The following is a script for my enemies in a 2D game that looks at certain points. The majority of the time it works, although every one in a while (1/5ish) the error. I can't find any way to replicate the error consistently, sometimes it throws once every time the coroutine runs, sometimes it throws multiple times. 99% sure the error isn't due to the layermasks or tagging, 100% sure not do the allWayPoints list. It is a little complex but any help is welcome.

 InvalidOperationException: Collection was modified; enumeration operation may not execute

It says the error is at line 778 in the file List.cs.

 //Needs work (buggy and glitchy sometimes)
 IEnumerator lookAround(float time)
 {
     visiblePoints.Clear();
 
     //Raycasting and adding to visiblePoints list
     foreach (GameObject obj in allWayPoints)
     {
         RaycastHit2D hit = Physics2D.Raycast(transform.position, obj.transform.position - transform.position, Mathf.Infinity, layerMask);
 
         if (hit.collider.tag == "LookPoints")
         {
             float dirAngle = Vector3.Angle(transform.right, (hit.transform.position - transform.position).normalized);
             Vector3 cross = Vector3.Cross(transform.right, (hit.transform.position - transform.position).normalized);
 
             //Checks which direction of angle
             if (cross.z < 0)
                 dirAngle = -dirAngle + 360;
 
             Waypoints newPoint = new Waypoints(obj.transform.position - transform.position, dirAngle, hit.distance);
 
             bool checker = false;
 
             //Makes sure visible points aren't too similar of an angle
             foreach (Waypoints item in visiblePoints)
             {
                 if (Mathf.Abs(dirAngle - item.eulerZ) < 20)
                     checker = true;
             }
 
             if (!checker)
                 visiblePoints.Add(newPoint);
         }
     }
 
     //Sorting
     visiblePoints.Sort();
 
     //Reversing if necessary
     if (360 - visiblePoints[visiblePoints.Count - 1].eulerZ < visiblePoints[0].eulerZ)
         visiblePoints.Reverse();
 
     //Looking at each point
     while (timer < time)
     {
         //So it runs at least twice
         for(int i = 0; i < 2; i++)
         {
             foreach (Waypoints item in visiblePoints)
             {
                 //Rotates to look at angle
                 while (Vector3.Angle(transform.right, item.direction) > 3)
                 {
                     transform.right = Vector3.RotateTowards(transform.right, item.direction, Time.deltaTime * rotSpeed, 0);
                     yield return null;
                 }
 
                 yield return new WaitForSeconds(1.1f);
             }
         }
     }
 
     //Reseting
     visiblePoints.Clear();
     lastKnownPos = resetPos;
     lastSuspectPos = resetPos;
     wasExecuted = false;
 }

Class code:

 public class Waypoints : IComparable<Waypoints>
 {
     public Vector3 direction;
     public float eulerZ;
     public float distance;
 
     //Gives class ability to be sorted
     public int CompareTo(Waypoints other)
     {
         if (this.eulerZ > other.eulerZ)
             return 1;
         else if (this.eulerZ < other.eulerZ)
             return -1;
         else
             return 0;
     }
 
     //Constructor
     public Waypoints(Vector3 dir, float z, float dis)
     {
         direction = dir;
         eulerZ = z;
         distance = dis;
     }
 }

I guess I'm kind of asking a lot here. Any help is great, as I don't fully understand lists or coroutines, which I suspect to the problem somehow. I'm really lost, the error is so inconsistent and I have no idea what is going on, or what the List.cs file is.

Comment
Add comment · Show 4
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 gtaharaedmonds · Nov 04, 2016 at 01:09 AM 0
Share

Any questions just ask, I probably forgot some important info.

avatar image Cynikal ♦ · Nov 04, 2016 at 02:06 AM 0
Share

What line exactly is the error? I realize you said it was line 778. However, We see 1 through 68 here.

avatar image gtaharaedmonds Cynikal ♦ · Nov 04, 2016 at 02:22 AM 0
Share

The error is in List.cs. I'm pretty sure the List.cs file is the code included for lists, I didn't write it myself. This code here is in EnemyAI script which is only about 250 lines.

avatar image gtaharaedmonds Cynikal ♦ · Nov 04, 2016 at 02:24 AM 0
Share

I think how it works is there are script for ints, floats, strings, etc. I believe List.cs is just another one of these. Not gonna lie don't really fully understand what is going on.

0 Replies

· Add your reply
  • Sort: 

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

77 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

Related Questions

Coroutine doesn't continue after yield another coroutine 0 Answers

Adding to List of Nested Classes 1 Answer

Confused as hell about static classes and stuff. 1 Answer

Using the "Contain" method on a list with various variables. 0 Answers

How do I implement lists for this? Or should I? 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