assertion failed on expression 'isInSyncWithParentSerializedObject()'

I have a for loop in my code:

        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:
9888435--1427097--upload_2024-6-13_15-13-57.png
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.

Sounds like a Unity bug to me especially because this was posted on the forums a few days ago: Assertion failed on expression: 'IsInSyncWithParentSerializedObject()' - Unity Engine - Unity Discussions

1 Like

I know, but I round the distance, (and the position of all the gameObjects)

What is call stack? But if I double press it, it does not lead me any where

9890265--1427514--upload_2024-6-14_13-53-58.png
In the debugger it shows me this

That’s not the error you reported above.

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."

Assuming “HorizontalMovement” is a List then you can do:

 for (var i = 0; i < HorizontalMovement.Count; ++i)
{
    var movement = HorizontalMovement[i]

    ...
    ...
    ...
}

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

At first, it showed me the error I have reported, and then (without any change in the code) the previous error disappeared, and this showed

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?