I am trying to make enemy spawn using InvokeRepeating but any how i am not able to stop it when i have enough number of enemy
Here is the code can any one tell me what went wrong.
public GameObject enemy; // The enemy prefab to be spawned.
public float spawnTime = 7.0f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
public int LevelMaxEnemy=10;
public int numofEnemy=0;
void Start ()
{
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn ()
{
// If the player has no health left...
if(PlayerMovementScript.playerHealth <= 0f)
{
// ... exit the function.
return;
}
if(numofEnemy > LevelMaxEnemy){
// ... exit the function.
return;
}
else{
// Find a random index between zero and one less than the number of spawn points.
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
numofEnemy = numofEnemy + 1;
}
}
↧