Unity reports an error invalidoperationexception: solution of out of sync

Time:2022-1-6

Invalidoperationexception of unity error: out of sync

Original error report:

InvalidOperationException: out of sync
System.Collections.Generic.Dictionary2+Enumerator[System.Int32,UnityEngine.Transform].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912) System.Collections.Generic.Dictionary2+Enumerator[System.Int32,UnityEngine.Transform].MoveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:278)
System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[System.Int32,UnityEngine.Transform].MoveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:1028)
BigHandCard+c__Iterator6.MoveNext () (at Assets/Scripts/Public/HandCards.cs:781)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

Checked on the Internet, which is caused by direct modification in the iterator. C# is that you are not allowed to modify it directly in the iterator.
This is a false demonstration

public void ShowMyCard(int[] card)
    {
        if (myCardsDic.Count > 0)
        {
            foreach (int k in myCardsDic.Keys)
            {
       If (one condition is met)
                   {
                        //Delete or modify this element
            //myCardsDic.Remove(k);
          }
            }
        }
    }

If there is such a requirement, it should be written like this, [General Logic: traverse the dictionary to store the elements that meet the conditions, and then operate the elements you just stored]

public void ShowMyCard(int[] card)
    {
        if (myCardsDic.Count > 0)
        {
            List<int> myCardsList = new List<int>();
            foreach (int k in myCardsDic.Keys)
            {
                If (one condition is met)
                   {
                        //Save this element
                     myCardsList.Add(myCardsDic[k]);
                }
            }
           foreach (int item in myCardsList)
            {
       //Do what you have to do
            }
        }
    }

The problems I encountered were different from those above

My problem: when traversing the dictionary, I used the co process, and then called the dictionary in other cases, resulting in the above error. The error code is roughly as follows:


    public IEnumerator ShowMyCard(int[] card)
    {
        if (myCardsDic.Count > 0)
        {
            foreach (int k in myCardsDic.Keys)
            {
                float x = myCardsDic[k].localScale.x;
                myCardsDic[k].DOScaleX(0, 0.02f).OnComplete(() =>
                {
                     myCardsDic[k].DOScaleX(x, 0.02f);
                });
                yield return new WaitForFixedUpdate();
            }
        }
    }

Solution to the problem I encountered: I didn’t use the collaborative process to operate in the process of use. I found that the effect was also good. The modified code is as follows:


    public void ShowMyCard(int[] card)
    {
        if (myCardsDic.Count > 0)
        {
            foreach (int k in myCardsDic.Keys)
            {
                float x = myCardsDic[k].localScale.x;
                myCardsDic[k].DOScaleX(0, 0.02f).OnComplete(() =>
                {
                     myCardsDic[k].DOScaleX(x, 0.02f);
                });
            }
        }
    }

You are modifying the dictionary while iterating over it. This is a big no-no.
You are modifying the dictionary and traversing it at the same time. This is a big taboo.

This is the end of this article about the solution of unity error invalidoperationexception: out of sync. For more information about unity error, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!