How can I change scene or Quit game after killing all the spawned enemies?
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");
         }
     }
 }
 
               }
Answer by Priyanka-Rajwanshi · Apr 06, 2018 at 05:57 AM
@shubhamshetkar25 Take a variable in EnemyManager(or any singleton in your project) stating the number of enemies killed.
     public int enemiesKilled;
 
               You would need an EnemyScript in which you check for enemy's health. This script would be present on all spawned enemies.
In EnemyScript, take a reference of EnemyManager
    EnemyManager enemyManager;
    int enemyHealth;
 
               and where you would check for enemy's health, write:
    if(enemyHealth <= 0){
       enemyManager.enemiesKilled++;
       if(enemyManager.enemiesKilled >= enemyManager.enemiedSpawned)
          //Change Scene
     }
 
              i am getting this error : Assets/_Complete-Game/Scripts/Enemy/EnemyHealth.cs(9,16): warning CS0649: Field CompleteProject.*EnemyHealth*.enemy$$anonymous$$anager' is never assigned to, and will always have its default value null'
EnemyHealth is my EnemyScript
and when i kill each enemy i gets this error
NullReferenceException: Object reference not set to an instance of an object CompleteProject.EnemyHealth.TakeDamage (Int32 amount, Vector3 hitPoint) (at Assets/_Complete-Game/Scripts/Enemy/EnemyHealth.cs:76) CompleteProject.PlayerShooting.Shoot () (at Assets/_Complete-Game/Scripts/Player/PlayerShooting.cs:111) CompleteProject.PlayerShooting.Update () (at Assets/_Complete-Game/Scripts/Player/PlayerShooting.cs:49)
enemy$$anonymous$$anager variable needs to be assigned. In Start() of EnemyHealth write
     enemy$$anonymous$$anager = GameObject.FindObjectIfType<Enemy$$anonymous$$anager>();
 
                   Otherwise you can make Enemy$$anonymous$$anager as Singleton and use its instance.
in this code what is enemiesSpawned ? when i tries to replace enemiesSpawned with maxEnemyCount as in my enemy$$anonymous$$anager script on top i can kill only 3 enemies after that scene changes.. but i have enemies of 3 types (3 enemies of each type)
 if(enemyHealth <= 0){
        enemy$$anonymous$$anager.enemies$$anonymous$$illed++;
        if(enemy$$anonymous$$anager.enemies$$anonymous$$illed >= enemy$$anonymous$$anager.enemiesSpawned)
           //Change Scene
      }
                   Your answer