Unity3D比较两个字符串时出现stackoverflow错误

问题描述 投票:0回答:1

所以我正在制作一个游戏,我目前正在研究库存系统,我创建了一个项目和一个库存类,我比较了库存类中项目的一些属性,当我尝试访问物品类别,这里是库存和物品类别:

public class item
{
    public string id { get; set; }
    public int amount;
    public int stacksize;
}

public class inventory
{
    public bool open = false;
    item[] items = new item[43];
    //first 30 inv
    //next 5 hotbar
    //next 4 armor
    //next 4 equipment
    public GameObject invScreen;

    public void printInv()
    {
        foreach (item n in items)
        {
            PlayerController.print(n.id + "   |   " + n.amount);
        }
    }

    public void addItem(item add)
    {
        int i = 0;
        foreach(item x in items)
        {
            item f = x;
            if (f.id == add.id)
            {
                if (add.amount + f.amount <= f.stacksize)
                {
                    items[i].amount += add.amount;
                    f.amount = 0;
                }
                else
                {
                    f.amount = (add.amount + f.amount) - f.stacksize;
                }
            }
            i++;
        }
        i = 0;
        foreach(item x in items)
        {
            item f = x;
            if (f.id == "filler")
            {   
                if (add.amount + f.amount <= f.stacksize)
                {
                    items[i].amount += add.amount;
                    f.amount = 0;
                }
                else
                { 
                    f.amount = (add.amount + f.amount) - f.stacksize;
                    addItem(f);
                }
            }
            i++;
        }
    }

    public void swap(int item1, int item2)
    {
        item x = items[item1];
        item y = items[item2];
        items[item1] = y;
        items[item2] = x;
    }

    public void init()
    {
        int i = 0;
        foreach(item v in items)
        {
            item e = new item();
            e.stacksize = 1;
            e.amount = 1;
            e.id = "filler";
            items[i] = e;
            i++;
        }
    }
}

我知道我的代码可能没有优化也没有组织,所以很抱歉……

我尝试搜索可能导致无限循环的代码,但我没有找到任何东西,所以这就是为什么我在这里....

c# string unity3d stack-overflow
1个回答
0
投票

好的,我解决了,谢谢,不需要再回答了:D

© www.soinside.com 2019 - 2024. All rights reserved.