So... i was working at this Wave System for a little game and i wanted the system to wait a specific amount of time before spawning another enemy, so i did this thing:
void ExecuteWaveAction(WaveAction action)
{
int numberOfSpawns = spawnLocations.Length;
int currentSpawnToInstantiate = 0;
float timeLeftToSpawnEnemy = 0f;
for (int i = 0; i < action.quantityOfEnemysToSpawn; i++)
{
if (timeLeftToSpawnEnemy < action.spawnRate)
{
timeLeftToSpawnEnemy += Time.deltaTime;
i--;
}
else
{
GameObject.Instantiate (action.enemyToSpawn, spawnLocations [currentSpawnToInstantiate].position, Quaternion.identity);
currentSpawnToInstantiate++;
timeLeftToSpawnEnemy = 0f;
if (currentSpawnToInstantiate >= numberOfSpawns)
currentSpawnToInstantiate = 0;
}
}
}
if you are asking yourself what a WaveAction is :
public struct WaveAction
{
public int quantityOfEnemysToSpawn;
public float spawnRate;
public GameObject enemyToSpawn;
}
i don't know what is wrong with the code, when i am debugging everything seems to be fine, the script actually waits before spawning, but when i am playing the script spawn all creatures at once.
if someone could help me i would be very grateful, and last of all, if i made any spelling or english mistakes i am sorry, i am not a native english speaker