public void StopParticle(PlayParticleInfo info) { if (!info.IsValid) {…
- User
public void StopParticle(PlayParticleInfo info) { if (!info.IsValid) { Debug.LogWarning("stop invalid particle"); return; }
codeforeach (var proxy in m_proxies) { if (proxy.PlayInfo.Equals(info)) { StopParticle(proxy); } } } 经常会报一个错误InvalidOperationException: Collection was modified; enumeration operation may not execute. 怎么修改呢private HashSet<CardGameParticleProxy> m_proxies = new (); - Assistant
The error you're encountering,
InvalidOperationException: Collection was modified; enumeration operation may not execute, typically occurs when you modify a collection (add, remove, or change elements) while iterating over it. In your code, this could happen ifStopParticle(proxy)modifiesm_proxies.To fix this, you need to avoid modifying the collection while iterating over it. Here's a common pattern to handle such situations:
- Collect the items to be modified (or removed) in a separate list while iterating.
- Modify the collection after the iteration is complete.
Here's how you might adjust your method: