**I am experimenting with Survival Shooter project and I am spawning 3 enemies of 3 different types
and i want to change scene or quit game after killing all the spawned enemies.**
**this is my EnemyManager script :**
using UnityEngine;
using UnityEngine.SceneManagement;
namespace CompleteProject
{
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth; // Reference to the player's heatlh.
public GameObject enemy; // The enemy prefab to be spawned.
public float spawnTime = 3f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
private int enemyCount = 0;
public int maxEnemyCount = 3;
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(playerHealth.currentHealth <= 0f)
{
// ... exit the function.
return;
}
// 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);
enemyCount++;
if(enemyCount>=maxEnemyCount){
CancelInvoke("Spawn");
}
}
}
}
,**I am spawning 3 enemies of 3 types and i want to change scene or quit the game after killing all the spawned enemies.**
**this is my EnemyManager Script :**
using UnityEngine;
using UnityEngine.SceneManagement;
namespace CompleteProject
{
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth; // Reference to the player's heatlh.
public GameObject enemy; // The enemy prefab to be spawned.
public float spawnTime = 3f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
private int enemyCount = 0;
public int maxEnemyCount = 3;
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(playerHealth.currentHealth <= 0f)
{
// ... exit the function.
return;
}
// 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);
enemyCount++;
if(enemyCount>=maxEnemyCount){
CancelInvoke("Spawn");
}
}
}
}
↧