- Home /
IAP button gives error : InvalidOperationException: Collection was modified
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.?
help please.
Thank you.
Can you show us your iap initialization code, and the buying code
Answer by jamesk5 · Mar 28, 2018 at 10:29 PM
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.
Solution:
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.
Nice solution $$anonymous$$an!, well unity did replicated the error, and its investigating it now as they tell me.
now I just replaced the IAP package with and older one I had in backup (gotta always have backups!).
if it happens again, I´ll know what to do. thanks!
You can also use a coroutine and just wait a single frame. Also seems to work.
That was it! Thanks! Ins$$anonymous$$d of disabling game object you can simply disable the component image or button or the canvas group that contains it. This can happen instantly without problem.
Answer by MacDx · Mar 07, 2018 at 10:37 PM
That error usually pops whenever you erase/add elements from/to a list that you are iterating over. Example:
foreach(Foo foo in fooList)
{
if(foo.shouldBeRemoved)
fooList.Remove(foo);
}
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)
{
if(foo.shouldBeRemoved)
fooToRemove.Add(foo);
}
foreach(Foo foo in fooToRemove)
{
fooList.Remove(foo);
}
//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 @$$anonymous$$acDx !
Answer by immodiumvash · Mar 09, 2018 at 07:36 AM
this is the error @chiker and @MacDx:
InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List`1+Enumerator[UnityEngine.Purchasing.IAPButton].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List`1+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+<>cDisplayClass15_0.b0 (Boolean allow, PurchaseFailureReason failureReason) UnityEngine.Purchasing.UIFakeStore+<>cDisplayClass14_0`1[UnityEngine.Purchasing.PurchaseFailureReason].b0 (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.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
So, neither IAPButton.cs, nor StoreListenerProxy.cs nor Purchasing$$anonymous$$anager.cs are yours right? Is there literally not a single one of your classes involved in this whole process? Because if it is so, then the error should be on the plugin side and you'd want to contact the developers. Are you passing any data whatsoever to this process?
Exaclty, its not my code in anyway. those buttons just unlock items, the only thing I pass is the result of the purchase witch just change a playerpref key and this works fine cose after I unpause the error in the editor everything its ok.ther was no error before the last update of the IAP Package. @$$anonymous$$acDx
Yup, I think your best bet is to report this to the developers of this package.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dictionary suddenly loses its items 1 Answer
Windows Universal IAP 0 Answers