虽然循环使 Unity 崩溃,但不是无限的

While loop crashing Unity, but is not infinite

我知道(从类似的帖子中)无限 while 循环因导致 Unity3d 崩溃而臭名昭著。我试图在我正在处理的内容中实现一个 while 循环,我很确定这不是 'infinite',但会导致游戏崩溃。

逻辑的想法是检查整数列表中的连续数字,并以此作为应用奖金的基础。该列表包含 'effective shots',并且每次射击都会添加一个新的 int - 连续有效射击越多,奖励越高。

这是我拥有的:

        int count = 0;
        int firstItem = 0;
        int multiples = 3;
        int currentMultiple = 0;
        int bonusX = 0;

        foreach (int i in effectiveShots)
        {
            if (count == 0)
            {
                firstItem = i;
                count = 1;
            }

            else if (i == firstItem + count)
            {
                count++;
            }

        }

        while (count >= multiples)
        {
            currentMultiple = multiples;

            if (count == currentMultiple + multiples)
            {
                bonusX += 1;
            }

            if (bonusX > 10 || gameOver)
                break;

            UnityEngine.Debug.Log(bonusX);
        }

检查 effectiveShots 列表中连续条目的逻辑取自@Jon Skeet 的回答 here。尽管这似乎可行,但我认为这可能是问题所在。一旦错过一枪,count 就需要重新设置。 有什么想法或建议

当连续有效投篮数达到第一个倍数,即3投后,进入while循环。然后,对于此后每一组连续有效的投篮,增加奖金,例如:

3 shots: bonusX = 1
6 shots: bonusX = 2
9 shots: bonusX = 3
12 shots: bonusX = 4

and repeat this until `count` is no longer >= 3, i.e. the player missed a shot. 

问题是当我连击3次进入这个循环后,游戏就崩溃了。我不认为我会称之为无限循环,因为错过一个镜头 - 设置 count == 0 - 意味着 while 条件不再为真,所以退出循环(我认为?)。我还添加了额外的检查以在某些情况下跳出循环,但这并没有什么区别。

如果您能够指导如何解决此崩溃问题,或者对总体上更好的方法提出建议,我们将不胜感激!

while 循环中的任何内容都不会更改 countmultiples 的值,因此条件 总是 的计算结果相同值 => 无限循环