'Why is my code spawning enemies wrong in the second Coroutine but not the first?
I'm working on my first game and want it to spawn enemies in pre-planned waves. I made a script to spawn them in a 5x3 grid, which worked fine. I then modified the code so it can spawn them in any 5xn grid where n is the number of rows you want. For some reason, the code works perfectly the first time I run a coroutine, but the second time it will misplace the enemies in the first two rows. This happens no matter the number of rows I choose, even if they are different numbers. My code is as follows:
IEnumerator BasicLine(int rows)
{
var spawnPos = enemyPos;
for (int j = 0; j < rows; j++)
{
for (int i = 0; i < 5; i++)
{
spawnPos.position = spawnPos.position + new Vector3(2.5f * i, 2 * j, 0);
Enemy1 = enemy1Pool.GetObject();
Enemy1.transform.position = enemyPos.position;
Enemy1.SetActive(true);
spawnPos.position = spawnPos.position - new Vector3(2.5f * i, 2 * j, 0);
}
}
yield return null;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
