motkot
@motkot
Программирование C#.

Как исправить ошибку?

Иногда не выдает.
Выдает эту ошибку:
InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Generic.List`1+Enumerator[T].MoveNextRare () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Generic.List`1+Enumerator[T].MoveNext () (at <695d1cc93cca45069c528c15c9fdd749>:0)
BoxController.OpenBox (System.Int32 BoxId) (at Assets/Scripts/BoxController.cs:112)

Код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

class Coins 
{
    public int Count;
    public Sprite image;
}

class PowerPoints
{
    public int PointsAmount;
    public Brawler brawler;
}

class Gems
{
    public int Count;
    public Sprite Image;
}

public enum Showing
{
    Coins,
    PowerPoints,
    Finish
}

class Items
{
    public Coins coins = new Coins();
    public List<PowerPoints> powerPoints = new List<PowerPoints>();
    public Brawler NewBrawler;
    public Gems gems = new Gems();
    public int BrawlerIndex;

}

public class BoxController : MonoBehaviour
{
    [SerializeField] Brawler[] Brawlers;
    [SerializeField] PlayerStats playerStats;

    [SerializeField] Box[] Boxes;

    [SerializeField] Text CoinsText;
    [SerializeField] Text PPText;
    [SerializeField] Text GemsText;
    [SerializeField] Text ItemsAmountText;

    [SerializeField] GameObject OpenButton;

    [SerializeField] GameObject Show;
    [SerializeField] Text ShowText;
    [SerializeField] Image ShowImage;

    [SerializeField] Sprite CoinsImage;

    float Number;
    int CoinsAmount;

    Showing WhatShowing;
    Items items = new Items();

    int PPAmount;

    bool HaveBrawler;

    public void OpenBox(int BoxId)
    {
        bool firstAtemp = true;
        for(int i = 0; i < Boxes[BoxId].RandomIterations; i++)
        {

            Number = Random.Range(0, 101);

            if (Number <= Boxes[BoxId].BonusChance)
            {
                int count = Random.Range(Boxes[BoxId].GemsAmount.x, Boxes[BoxId].GemsAmount.y);
                items.gems.Count += count;
                playerStats.Gems += count;
            }

            Number = Random.Range(0f, 100f);

            if(Number < Boxes[BoxId].CoinsAndPPChance)
            {
                Number = Random.Range(0, 2);

                if(Number == 0)
                {
                    CoinsAmount += Random.Range(Boxes[BoxId].CoinsAmount.x, Boxes[BoxId].CoinsAmount.y);
                    items.coins.Count += CoinsAmount;
                }
                else
                {
                    if(playerStats.brawlers.Count != 0)
                    {
                        Brawler PPBrawler = playerStats.brawlers[Random.Range(0, playerStats.brawlers.Count)];
                        int count = Random.Range(Boxes[BoxId].PPAmount.x, Boxes[BoxId].PPAmount.y);

                        if(firstAtemp)
                        {
                            items.powerPoints.Add(new PowerPoints() { brawler = PPBrawler, PointsAmount = count });
                            firstAtemp = false;
                        }

                        if (items.powerPoints.Count != 0)
                        {
                            foreach (PowerPoints points in items.powerPoints)
                            {
                                if (points.brawler == PPBrawler)
                                {
                                    points.brawler.PowerPoints += count;
                                }
                                else
                                {
                                    items.powerPoints.Add(new PowerPoints() { brawler = PPBrawler, PointsAmount = count });
                                }
                            }

                            PPBrawler.PowerPoints += count;

                        }
                        else
                        {
                            items.powerPoints.Add(new PowerPoints() { brawler = PPBrawler, PointsAmount = count });
                        }
                    }
                }


            }
            else if(!HaveBrawler)
            {
                Number = 100 - Number;

                if(Number <= Boxes[BoxId].LegendaryBrawlerChance)
                {
                    GetBrawler(Rarity.Legendary, BoxId);
                    continue;
                }

                if (Number <= Boxes[BoxId].MythicalBrawlerChance)
                {
                    GetBrawler(Rarity.Mythical, BoxId);
                    continue;
                }

                if (Number <= Boxes[BoxId].EpicBrawlerChance)
                {
                    Number = Random.Range(0, 2);
                    if(Number == 0)
                    {
                        GetBrawler(Rarity.Epic, BoxId);
                        continue;
                    }
                    else
                    {
                        GetBrawler(Rarity.Chromatic, BoxId);
                        continue;
                    }
                }

                if (Number <= Boxes[BoxId].SuperRareBrawlerChance)
                {
                    GetBrawler(Rarity.SuperRare, BoxId);
                    continue;
                }

                if (Number <= Boxes[BoxId].RareBrawlerChance)
                {
                    GetBrawler(Rarity.Rare, BoxId);
                    continue;
                }


            }

            

            playerStats.Money += CoinsAmount;
            playerStats.PowerPoints += PPAmount;

            CoinsAmount = 0;
            PPAmount = 0;
            //GemsText.text = "Гемы: " + playerStats.Gems;
            //CoinsText.text = "Монетки: " + playerStats.Money;
            //PPText.text = "Павер поинты: " + playerStats.PowerPoints;
        }
        Show.SetActive(true);

        int ItemsAmount = 0;

        if (items.coins.Count != 0)
        {
            ItemsAmount++;
            if (items.powerPoints.Count != 0)
            {
                ItemsAmount++;
                if (items.NewBrawler != null)
                {
                    ItemsAmount++;
                    if (items.gems.Count != 0)
                    {
                        ItemsAmount++;
                    }
                }
            }
        }

        ItemsAmountText.text = ItemsAmount.ToString();

        if (items.coins.Count != 0)
        {
            ShowImage.sprite = CoinsImage;
            ShowText.text = items.coins.Count.ToString();
            WhatShowing = Showing.PowerPoints;
        }
        else if (items.powerPoints.Count != 0)
        {
            ShowImage.sprite = items.powerPoints[items.BrawlerIndex].brawler.image;
            WhatShowing = Showing.PowerPoints;
        }
    }
    
    private void GetBrawler(Rarity rarity, int BoxId)
    {
        foreach(Brawler brawler in playerStats.brawlers)
        {
            if(brawler != null)
            {
                foreach(Brawler AllBrawler in Brawlers)
                {
                    if (brawler == AllBrawler && AllBrawler.rarity == rarity)
                    {
                        Number = Random.Range(0f, 100 - Boxes[BoxId].CoinsAndPPChance);
                        return;
                    }
                }

            }
            else
            {
                break;
            }
        }

        foreach(Brawler AllBrawler in Brawlers)
        {
            if(AllBrawler.rarity == rarity)
            {
                playerStats.brawlers.Add(AllBrawler);
                items.NewBrawler = AllBrawler;
                HaveBrawler = true;
                return;
            }
        }
    }

    public void Next()
    {
        if(WhatShowing == Showing.Coins || items.powerPoints.Count != 0 && items.powerPoints.Count != items.BrawlerIndex)
        {
            ShowImage.sprite = items.powerPoints[items.BrawlerIndex].brawler.image;
            ShowText.text = items.powerPoints[items.BrawlerIndex].PointsAmount.ToString();
            items.BrawlerIndex++;
            WhatShowing = Showing.PowerPoints;
        }
        else if(WhatShowing == Showing.PowerPoints && items.NewBrawler != null)
        {
            ShowImage.sprite = items.NewBrawler.image;
            ShowText.text = items.NewBrawler.name;
        }
        else if(items.gems.Count != 0 && WhatShowing != Showing.Finish)
        {
            ShowImage.sprite = items.gems.Image;
            ShowText.text = items.gems.Count.ToString();
            WhatShowing = Showing.Finish;
            return;
        }
        else
        {
            WhatShowing = Showing.Finish;

            if (WhatShowing == Showing.Finish)
            {
                Show.SetActive(false);
                items = new Items();

                HaveBrawler = false;
                GemsText.text = "Gems: " + playerStats.Gems;
                CoinsText.text = "Coins: " + playerStats.Money;
                PPText.text = "Power points: " + playerStats.PowerPoints;
            }

            return;
        }
    }
}
  • Вопрос задан
  • 1765 просмотров
Решения вопроса 1
Нельзя модифицировать коллекцию, по которой проходишься оператором foreach
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы