How to prevent infinite loop when Array Index Is Out Of Range

Basically, my problem is that I’m using variable “i” to flip through an array of spawn points. When i becomes greater than the amount of spawn points available, “i” obviously leaves the range of the spawn point array. My solution is to simply reset “i” inside the scope of the for loop, but this doesn’t seem to solve my problem. What’s going on?

function spawnWave()
{
	if(waveActive)
	{
		for (var i=0; i<=waveNumber; i++)
		{
			if (i>8)
			{
				i = 0;
				print(i);
			}
			Instantiate(enemy1, Vector3(spawnArray<em>.transform.position.x, spawnArray_.transform.position.y, spawnArray*.transform.position.z), Quaternion.identity);*_</em>

* //something to delay spawning for more than six*
* }*
* waveActive = false;*
* needSpawn = false;*
* }*
* else*
* {*
* yield WaitForSeconds(1);*
* waveActive = true;*
* }*
}

you can use Invoke(“callYourFunction”, 3) for example, it will call the function in 2 seconds.
In your case you can change the value of waveActive = true; inside a function and call the function from Invoke, hope it helps!

If you want to iterate through all elements of spawnArray, it’s easier to do the following:

   for (var spawnPt in spawnArray){
     Instantiate(enemy1, spawnPt.transform.position, Quaternion.identity);
   }

But if you need the index, use the array length in a regular for loop:

   for (var i=0; i < spawnArray.length; i++){
     Instantiate(enemy1, spawnArray*.transform.position, Quaternion.identity);*

}
Anyway, you don’t need to create a new Vector3 with the position values - position itself is a Vector3, thus you can use it directly.