Invoke repeating?

Hello. This is my script I am using to spawn enemies in my game. I want to make it so that enemies will spawn every few seconds until the number of objects with the “Enemy” tag exceeds the number of the “Projectile.Defeat” number exceeds the specified value. I tried adding “Invoke repeating”, and I get no errors, but now no enemies spawn at all, how can I fix my script? Thanks

var objs : GameObject[];
var gos : GameObject[];
	gos = GameObject.FindGameObjectsWithTag("Enemy");

InvokeRepeating("LaunchProjectile", 2, 0.3);

function LaunchProjectile () {

if (Projectile.Defeat < 30)
	{yield WaitForSeconds(2.0);
	for (var a = 0; a < 3; a++)		
		Instantiate(objs[(Random.Range(0, 3))], new Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663)), Quaternion.identity);
			if(gos.length > 20)
				{
				}
	}
}

Ok.

  1. Put InvokeRepeating and gos = … in the Start() or Awake() functions. That is Unity’s designated initializer, whether it works as is or not, it makes more sense to put them in a function.

  2. You do not need to yield another 2 seconds after you call the LaunchProjectile method. Just change the parameters in InvokeRepeating().

Thanks :slight_smile: