Adding Multiple Keys/Values to a hashtable inside Foreach Loop

I am working on synchronization for my inventories/chests.

Apparently the easiest data I can transfer over photon RPC is a Hashtable.
So Foreach item in my inventory, I am trying to create a temporary HT, and add ID and Amount Keys, with their value’s. But it doesn’t seem to like me adding two keys inside of a for loop.

			foreach (Item _item in inventory) {
				ExitGames.Client.Photon.Hashtable _itemInfo = new ExitGames.Client.Photon.Hashtable ();
				_itemInfo.Add ("ID", _item.itemID);
				_itemInfo.Add("Amount", _item.itemAmount);
				photonView.RPC ("SendItemInfo", PhotonTargets.All, _itemInfo, photonView.viewID);
			}

This want’s to give me: InvalidOperationException: Collection was modified; enumeration operation may not execute.

I don’t know if the Photon Hashtable warks as a regular one, but if that’s the case, you don’t have to add the ID and then the item, you add both at a time. I’d change this two lines

_itemInfo.Add ("ID", _item.itemID);
_itemInfo.Add("Amount", _item.itemAmount);

for

_itemInfo.Add(_item.itemID, _item.itemAmount);

Hope this helps

Possibly the RPC is modifying inventory. One way to fix this is to make a copy of inventory, then iterate over that copy.

During the loop your inventory is being changed indirectly

just try foreach (Item _item in inventory.ToList()) also add system.linq as ToList() exits in Linq

ExitGames.Client.Photon.Hashtable props = null;

        props = new ExitGames.Client.Photon.Hashtable
        {
            {"IsPlayerState",playerState},
             {"GW_Count",GlobalConstant.GW_Count}
        };
    }
PhotonNetwork.LocalPlayer.SetCustomProperties(props);