foreach (Movement i in HorizontalMovement)
{
if (i.Move[3].strongSide.Contains(2))
{
foreach (RaycastHit2D b in i.FixedRightyRay)
{
if (b.distance == 0)
{
if (!(Parents.Contains(this) && Parents.Count == 1))
{
Horizontal.Add(i.Move[3].gameObject);
HorizontalMovement.Add(i.Move[3]);
}
}
else break;
}
}
}
I am using debug mode, it doesn’t effect the bug, but it is easier to see it that way.
It shows me an error at the first line, and this is the error:
What does it mean?
It has nothing to do with the script you posted. Does the message have a callstack? (select it)
Btw are you sure you meant to write “else break;” there? This looks like a glaring bug where the inner foreach loop doesn’t do anything except when the first RaycastHit2D happens to have a distance of precisely 0. Since distance is a float, you’ll also miss any hits at 0.000001f distance.
It’s likely because you’re enumerating (via ForEach) “HorizontalMovement” and you’re adding to it with “HorizontalMovement.Add(i.Move[3]);”
As it states, you cannot modify a collection you’re enumerating.
"An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined."
This error was happening to me, in my case I had arrays exposed in the inspector that I was populating via a script,
I had to hide them in the inspector for the error to go away, this started to happen after I upgraded Unity
from 2022.3.16f1 to 2022.3.33f1, on the same project, not sure if it’s relevant but wasn’t happening before the upgrade,
I already had this script before upgrading.
Rope it helps
Regards
That wasn’t really the main thing I was replying about. What about a comment on the problem with your code and how it causes the error you posted? Did that help? Did you understand it?