繁体   English   中英

C# 统一。 当值为 2 时,控制变量会导致循环

[英]C# Unity. Control variable causes loop when value is 2

我有这个代码,当变量waveNumer等于 1 时,代码工作得很好,会产生敌人等等,但是当 2 Unity 崩溃时。 我猜这正在进入一个堰循环,但我无法弄清楚它为什么会发生。

        int percentForWave=0;
        int percentForType=0;

        int TotalEnemies = (int)enemySpawnsThisRound;
        if (waveNumer == 1)
        {
            Debug.Log("Entro al wave 1");
            percentForWave = 20;
            percentForType = 20;
            startList = 0;

        }
        if (waveNumer == 2)
        {
            Debug.Log("Entro al wave 2");
            percentForWave = 70;
            percentForType = 70;
            startList = endList;

        }
        if (waveNumer == 3)
        {
            Debug.Log("Entro al wave 3");
            percentForWave = 10;
            percentForType = 10;
            startList = endList;
        }


        int enemiesThisWave = Decimal.ToInt32(Math.Round(TotalEnemies * ((decimal)percentForWave / 100), 1));
        int enemiesForType = Decimal.ToInt32(Math.Round(lenghtList * ((decimal)percentForType / 100), 1));


        endList = enemiesForType + startList;

        clonesASpawnear = new GameObject[enemiesThisWave];
        int i = 0;

        while ( i < clonesASpawnear.Length)
        {



            for (int j = startList; j == endList; j++)
            {
                Debug.Log("Numero j = " + j);
                if (clonesASpawnear[i] == null)
                {

                    clonesASpawnear[i] = Instantiate(enemyTypeList[j], spawnPoints[j].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
                    clonesASpawnear[i].SetActive(true);//lo activo
                    clonesASpawnear[i].GetComponent<EnemyMovement_DCH>().player = Target;
                    aliveEnemies += 1;
                    clonesASpawnear[i].GetComponent<EnemyDamageHandler_DCH>().SpawnerEnemies = this;
                    i++;
                }



            }   

        }         

如果我能在程序崩溃后看到统一日志,但不知道如何做到这一点,这也会很有用。

从这里我可以看到我在你的 for 循环的条件语句中看到了一个问题,在这里你有

for (int j = startList; j == endList; j++)

在你的条件下,你只有一个才能得到是true j的值等于endList因为在您的 for 循环中j每次迭代都在递增时,您的条件只会在一次迭代中为真,我猜是根据您的第一次迭代。 如果 for 循环不会迭代i值, i是外部 while 循环的控制变量永远不会增加,因为您的 while 循环将进入无限状态,它永远不会停止迭代,一切都会冻结并崩溃.

正如我在评论中看到的那样,当j = 1j = 1 j < englist立即导致崩溃

我想在崩溃时获得一些变量的值。

  • clonesASPawnear.Length
  • 开始列表
  • 结束列表
  • 一世

我们最终以这种方式解决了这个问题。 如果有人在同一个泡菜中,我会发布解决方案。

 while ( i < clonesASpawnear.Length)
    {


        if(j <= endList)
        {
            Debug.Log("Numero j = " + j);
            //se fija que ya no este en el escenario el que va en ese punto asi no superpone items
            if (clonesASpawnear[i] == null)
            {

                clonesASpawnear[i] = Instantiate(enemyTypeList[j], spawnPoints[j].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
                clonesASpawnear[i].SetActive(true);//lo activo
                                                   //Aca le asigno el blanco que quiero que siga al spawnear
                clonesASpawnear[i].GetComponent<EnemyMovement_DCH>().player = Target;
                aliveEnemies += 1;
                //aca le asigno este spawner para que pueda afectar luego las variables cuando lo maten por ejemplo
                clonesASpawnear[i].GetComponent<EnemyDamageHandler_DCH>().SpawnerEnemies = this;

            }
            j++;
            i++;
        }
        else
        {
            j = startList;
        }







     Debug.Log("Numero i = " + i);

    }



}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM