C# Unity.当值为 2 时,控制变量会导致循环



我有这段代码,当变量waveNumeric等于 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++;
}

}   
}         

此外,如果我可以在程序崩溃后看到 unity 日志,但不知道如何做到这一点,那将很有用。

从这里我可以看到我看到你的 for loop 的条件语句中存在问题,看到这里有

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

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

正如我在评论中读到的那样,j < englistj = 1时立即导致崩溃

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

  • clonesas-pawnear.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);
}

}

最新更新