Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by shubhamshetkar25 · Apr 06, 2018 at 01:55 AM · instantiateenemyspawning problemsmanagerenemy spawn

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");
         }
     }

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
     }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shubhamshetkar25 · Apr 06, 2018 at 06:56 PM 0
Share

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)

avatar image Priyanka-Rajwanshi shubhamshetkar25 · Apr 07, 2018 at 02:30 AM 1
Share

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.

avatar image shubhamshetkar25 Priyanka-Rajwanshi · Apr 07, 2018 at 04:04 AM 0
Share

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
      }


Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate the whole object with script attached to it ? 0 Answers

Survival Shooter Spawn Enemies in Waves? 0 Answers

How do I spawn a certain amount of objects randomly between 2 positions? 0 Answers

How to spawn multiple prefabs at different spawn rates 0 Answers

Glitched character problem - Urgent Help Needed! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges