[英]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 = 1
時j = 1
j < englist
立即導致崩潰
我想在崩潰時獲得一些變量的值。
我們最終以這種方式解決了這個問題。 如果有人在同一個泡菜中,我會發布解決方案。
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.