Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug assertion failed on expression 'isInSyncWithParentSerializedObject()'

Discussion in 'Scripting' started by belpory, Jun 13, 2024.

  1. belpory

    belpory

    Joined:
    Mar 21, 2024
    Posts:
    29
    I have a for loop in my code:
    Code (CSharp):
    1.    
    2.         foreach (Movement i in HorizontalMovement)
    3.          {
    4.                 if (i.Move[3].strongSide.Contains(2))
    5.                 {
    6.                     foreach (RaycastHit2D b in i.FixedRightyRay)
    7.                     {
    8.                         if (b.distance == 0)
    9.                         {
    10.                             if (!(Parents.Contains(this) && Parents.Count == 1))
    11.                             {
    12.                                 Horizontal.Add(i.Move[3].gameObject);
    13.                                 HorizontalMovement.Add(i.Move[3]);
    14.                             }
    15.                         }
    16.                         else break;
    17.                     }
    18.                 }
    19.             }
    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:
    upload_2024-6-13_15-13-57.png
    What does it mean?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    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.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    WoozyBytes likes this.
  4. belpory

    belpory

    Joined:
    Mar 21, 2024
    Posts:
    29
    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
     
  5. belpory

    belpory

    Joined:
    Mar 21, 2024
    Posts:
    29
    upload_2024-6-14_13-53-58.png
    In the debugger it shows me this
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    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.

    https://learn.microsoft.com/en-us/d...#System_Collections_IEnumerable_GetEnumerator

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

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    Assuming "HorizontalMovement" is a List<T> then you can do:
    Code (CSharp):
    1.  for (var i = 0; i < HorizontalMovement.Count; ++i)
    2. {
    3.     var movement = HorizontalMovement[i]
    4.  
    5.     ...
    6.     ...
    7.     ...
    8. }
     
  8. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    10
    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
     
  9. belpory

    belpory

    Joined:
    Mar 21, 2024
    Posts:
    29
    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
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    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?