For循环未执行所有代码-没有错误

时间:2019-07-08 05:45:40

标签: c# unity3d math

https://simmer.io/@JammerLamma/~c09dd45e-0528-01ce-e641-e4a56fb9cfed

https://github.com/JammerLamma/Number-Muncher-Clone

我正在创建一个数字munchers克隆,并且整数变量未正确分配。它适用于一些游戏,但是在赢得几次和/或单击“退出”并选择另一个游戏后,似乎for循环正常进行了。游戏说有5个maxCorrect,而且correctOnBoard也设置为5,所以我假设循环经过5次,但是,只有4个图块被设置为正确,然后有时是2个或4个,但再也没有5个。它始终可以按预期工作在第一款游戏中。

如果我注释掉// AssignRest();问题仍然存在。

public void GenerateEven()
{
    //GameObject smasher = Instantiate(smasherPF, transform.position, transform.rotation);
    remainingNumbers = totalTiles - maxCorrect;
    GetLists();
    GetTiles();
    AssignEven();
    RemoveDuplicates(evenNumbers);
    AssignRest();
}

private void AssignEven()
{
    int maxCorrect = 5;
    for (int i = 1; i <= maxCorrect; i++)
    {
        randomChild = children[UnityEngine.Random.Range(0, children.Count)];
        int maxLength = evenNumbers.Count;
        int tempTileNum = evenNumbers[UnityEngine.Random.Range(0, maxLength)];
        randomChild.GetComponent<Tile>()._TileNumber = tempTileNum;
        randomChild.GetComponent<Tile>()._IsCorrect = true;
        correctOnBoard++;
        randomChild.GetComponent<SpriteRenderer>().sprite = numberSprite[tempTileNum - 1];
        Debug.Log(tempTileNum);
    }
}

为了调试目的,有些东西被序列化了。所有列表都会正确生成。

1 个答案:

答案 0 :(得分:0)

将代码更改为

private void AssignEven()
{
    for (int i = 1; i <= maxCorrect;)
    {
        randomChild = children[UnityEngine.Random.Range(0, children.Count)];

        if(randomChild.GetComponent<Tile>()._IsCorrect == false)
        {
            int maxLength = evenNumbers.Count;
            int tempTileNum = evenNumbers[UnityEngine.Random.Range(0, maxLength)];
            randomChild.GetComponent<Tile>()._TileNumber = tempTileNum;
            randomChild.GetComponent<Tile>()._IsCorrect = true;
            correctOnBoard++;
            randomChild.GetComponent<SpriteRenderer>().sprite = numberSprite[tempTileNum - 1];
            Debug.Log(tempTileNum);
            i++;
        }
    }
}

感谢约翰·吴!