0

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.

4
  • 2
    "It crashes" is not a sufficient problem description. What is the Exception message? What line does the debugger say it comes from? Feb 3, 2020 at 16:40
  • 4
    At a first look, I suspect that you want j < endList, not j == endList
    – Andrea
    Feb 3, 2020 at 16:40
  • I cant provide aditional information because the program just crashes when the variable value reaches 2. And no debug information is stored. Feb 3, 2020 at 16:52
  • when J<endList the program crashes right away (when the value is 1). Feb 3, 2020 at 16:54

2 Answers 2

2

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
3
  • 1
    I'm with @Ankit on this one. If you get no error messages/debug info, it is likely that infinite loop situation caused by unintended looping problems. Changing your j for loop should get you a step closer to what you'd want.
    – OliveJuice
    Feb 3, 2020 at 17:26
  • I cant see any values at the time of the crash. No logs are printed Feb 3, 2020 at 19:17
  • But this gave me enough idea of what is wrong to continue by myself, so tks a lot! Feb 3, 2020 at 21:12
0

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

    }



}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.