简体   繁体   中英

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

I have this code, when the variable waveNumer equals 1 the code works just fine, spawns the enemies and all that, but when is 2 Unity crashes. I'm guessing that is entering a weir loop but I cant figure it out why its happening.

        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++;
                }



            }   

        }         

Also It would be usefull if I could see the unity log after the program crashes, but no idea on how to do that.

From here what I can see I see a problem in your for loop's condition statement, see here you have

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

in your condition, you only have a single to get is true . when the value of j is equal to endList since in your for loop j is incrementing each and every iteration your condition will only be true for only one iteration which I guess the first iteration according to you. And if the for loop is not going to iterate value of i which is a control variable of an outer while loop is never going to increment hance your while loop will go to infinite-state where it'll never stop iterating, everything freezes and crash.

as I can read in comment that j < englist immediately result in a crash when j = 1

I would like to have the value of a few of the variables at the time of the crash.

  • clonesASpawnear.Length
  • startList
  • endList
  • i

We ended up solving the issue this way. I post the solution in case anyone is in the same pickle.

 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);

    }



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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