Hi Everyone, Im getting this error every time i click on an IAP button in my game :
“InvalidOperationException: Collection was modified; enumeration operation may not execute.”
and it does not reference any code in the game, it all seem to be an error in the code of the Unity IAP. the buttons were fine before I updated the IAP package.
now when i click an IAP button to buy an item in my game the editor pauses for this error, but if I unpause it it continues normaly as if the button worked. I dont know what to do as the error is not in my code. can I go back to an IAP earlier code.?
To follow my comment above, I found the answer (for me, at least).
This was happening because I was deactivating the UI where the IAP button was nested. Makes sense to deactivate your custom-purchase-confirmation-window (gameObject) when the purchase was completed, right? Well it throws this error.
Inside your Purchase Complete and Purchase Failed callbacks (the functions you assign in the inspector), don’t deactivate the IAP button (or its parent via hierarchy). Instead, put that deactivate-code into a separate function that you Invoke after 0.1 seconds. This lets the IAP button finish its business before it gets deactivated via the hierarchy.
That error usually pops whenever you erase/add elements from/to a list that you are iterating over. Example:
foreach(Foo foo in fooList)
This will give error since you are shortening the list while iterating over it, which can give unexpected results. If that’s the case what you can do is save whatever objects you want to delete inside a new list and then iterate over that list, deleting them from the original list.
List<Foo> fooToRemove = new List<Foo>();
foreach(Foo foo in fooList)
foreach(Foo foo in fooToRemove)
//You'd take the same approach for adding new elements
That’s my guess, however the only way I can be sure that this is the exact problem is if I take a look at your code.
Thanks for the answer man! , I get what the error is about, but its not in my code, I could not show you as is a code Im guessin of the unity IAP package. It happens when I click on a unity codeless IAP Button.
Ill post the exact error when I get home.
Thank you very much!
this is the error @chiker and @MacDx:
InvalidOperationException: Collection was modified; enumeration operation may not execute.
1+Enumerator[UnityEngine.Purchasing.IAPButton].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List1+Enumerator[UnityEngine.Purchasing.IAPButton].MoveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:784)
UnityEngine.Purchasing.IAPButton+IAPButtonStoreManager.ProcessPurchase (UnityEngine.Purchasing.PurchaseEventArgs e) (at Assets/Plugins/UnityPurchasing/script/IAPButton.cs:328)
UnityEngine.Purchasing.StoreListenerProxy.ProcessPurchase (UnityEngine.Purchasing.PurchaseEventArgs e) (at C:/buildslave/unity/build/Extensions/UnityPurchasing/Runtime/Purchasing/StoreListenerProxy.cs:34)
UnityEngine.Purchasing.PurchasingManager.ProcessPurchaseIfNew (UnityEngine.Purchasing.Product product) (at C:/buildslave/unity/build/Extensions/UnityPurchasing/Runtime/Purchasing/PurchasingManager.cs:212)
UnityEngine.Purchasing.PurchasingManager.OnPurchaseSucceeded (System.String id, System.String receipt, System.String transactionId) (at C:/buildslave/unity/build/Extensions/UnityPurchasing/Runtime/Purchasing/PurchasingManager.cs:115)
UnityEngine.Purchasing.JSONStore.OnPurchaseSucceeded (System.String id, System.String receipt, System.String transactionID)
UnityEngine.Purchasing.FakeStore.<>n__0 (System.String id, System.String receipt, System.String transactionID)
UnityEngine.Purchasing.FakeStore+<>c__DisplayClass15_0.b__0 (Boolean allow, PurchaseFailureReason failureReason)
1[UnityEngine.Purchasing.PurchaseFailureReason].b__0 (Boolean result, Int32 codeValue) UnityEngine.Purchasing.UIFakeStore.OkayButtonClicked () UnityEngine.Purchasing.UIFakeStore.b__16_1 () UnityEngine.Events.InvokableCall.Invoke (System.Object args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)