0

Here, I make a copy of the dictionary before iterating over it, and it still gives me this error. If anyone could help me that would be super awesome.

IEnumerator HandleStaticObjects()
{
    handleStaticObjectsIsRunning = true;

    List<string> instantiatedObjects = new List<string>();

    Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = staticObjectsWaitingToBeInstantiated;

    foreach (KeyValuePair<string, staticObjectWaitingToBeInstantiated> staticObject in currentStaticObjectsWaitingToBeInstantiated)
    {
        if (staticObject.Value.staticObject == null)
        {
            AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(@"C:/gameAssets/" + staticObject.Value.name.Split('?')[0] + ".unity3d");
            UnityEngine.Debug.Log("loading " + staticObject.Value.name.Split('?')[0]);
            yield return bundle;
            AssetBundle assetBundle = bundle.assetBundle;
            var assetLoadRequest = assetBundle.LoadAssetAsync<GameObject>(staticObject.Value.name.Split('?')[0]);
            yield return assetLoadRequest;

            staticObject.Value.staticObject = assetLoadRequest.asset as GameObject;
            assetBundle.Unload(false);
        }
        staticObjects[staticObject.Key] = Instantiate(staticObject.Value.staticObject);
        staticObjects[staticObject.Key].transform.eulerAngles = staticObject.Value.eulerAngles;
        staticObjects[staticObject.Key].transform.position = staticObject.Value.position;
        staticObjects[staticObject.Key].transform.localScale = staticObject.Value.localScale;
        if (staticObjects[staticObject.Key].GetComponent<DataHolder>() == null)
        {
            staticObjects[staticObject.Key].AddComponent<DataHolder>();
        }  
        staticObjects[staticObject.Key].GetComponent<DataHolder>().objectName = staticObject.Value.name;

        instantiatedObjects.Add(staticObject.Key);
    }
    foreach (string instantiatedObject in instantiatedObjects)
    {
        staticObjectsWaitingToBeInstantiated.Remove(instantiatedObject);
    }
    handleStaticObjectsIsRunning = false;
}

Stack Trace:

InvalidOperationException: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[System.String,CoOrdsHandler+staticObjectWaitingToBeInstantiated].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912)
System.Collections.Generic.Dictionary`2+Enumerator[System.String,CoOrdsHandler+staticObjectWaitingToBeInstantiated].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:835)
CoOrdsHandler+<HandleStaticObjects>c__Iterator0.MoveNext () (at Assets/Scripts/CoOrdsHandler.cs:342)
Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
  • Your problem statement should be in the start of question. Keep your question short and smart, and always format properly so others can read properly. – Abubakr Dar Jan 26 '16 at 01:04

1 Answers1

2

Sorry can't comment or i would but you're not making a copy, your creating a reference to the original.

change:

Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = staticObjectsWaitingToBeInstantiated;

to

Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = new Dictionary<string, staticObjectWaitingToBeInstantiated> (staticObjectsWaitingToBeInstantiated);

How do I copy the content of a dictionary to an new dictionary in C#?

Community
  • 1
  • 1
Muckeypuck
  • 549
  • 2
  • 14