首页 > 解决方案 > 检查列表是否有重复项

问题描述

我正在尝试进行检查并运行一个方法,直到它为真。

    public struct Letter
{
    public string letter;
}

private void DoStart()
{
    UsedIndexes.Clear();
    BuildGoodGame();
}

List<Letter> usedLetter = new List<Letter>();
private void BuildGoodGame()
{
    usedLetter.Clear();
    Letter temp;
    for (int i = 0; i < Couch.Length; i++)
    {
        do
        {
            couchIndex = CouchNameRandom.getRandom();
            Couch[i].setCurrentChildIndex(couchIndex);
            MisingLetterScript thisLetter = Couch[i].CurrentChild.GetComponent<MisingLetterScript>();
            string s = thisLetter.Letter;
            temp = new Letter
            {
                letter = s
            };
        } while (UsedNames.Contains(couchIndex) || usedLetter.Contains(temp));
        UsedNames.Add(couchIndex);
        usedLetter.Add(temp);
    }
}

我想运行这段代码两次,第一次运行正常,它完全完美。第二次,我执行 Invoke("DoStart", 1f) 并且它不再运行,通常冻结 Unity。整个问题在于 returnable 方法,它检查列表是否包含重复项。我不明白为什么第一次工作正常,然后我再次调用 DoStart 方法后,它就冻结了。帮助!!谢谢你。

标签: c#unity3d

解决方案


推荐阅读