Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Reefer · Jul 19, 2013 at 11:31 PM · gameobjectsceneenemylevel

How to keep count of enemies left

How I can check how many gameobjects with tag: "enemy" there is left in the scene, and after that it gets to zero then it will change level to another scene. I'm learning c# so I would prefer help on that language.

Comment
Add comment · Show 2
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 NEWBIE_1 · Jun 26, 2015 at 05:14 PM 0
Share

Did you get a script that loads a new level when your enemies are dead?

avatar image SleightOfCode NEWBIE_1 · Aug 29, 2016 at 01:44 AM 0
Share

Change void OnGui() to look like...

 void OnGUI()
      {
          if(killedAllEnemies)
          {
          Application.LoadLevel("YourLevelNameHere");
          }
          else
          {
              GUI.Label(new Rect (0,0,200,20),"Enemies Remaining : " + enemiesLeft);
          }
      }

2 Replies

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

Answer by bubzy · Jul 20, 2013 at 12:00 AM

 using UnityEngine;
 using System.Collections;
 
 public class gameobjects : MonoBehaviour {
 
     // Use this for initialization
     int enemiesLeft = 0;
     bool killedAllEnemies = false;
     void Start () {
         enemiesLeft = 10; // or whatever;
 
     }
     
     // Update is called once per frame
     void Update () {
     GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
         enemiesLeft = enemies.Length;
         if(Input.GetKeyDown(KeyCode.A))
         {
             enemiesLeft --;
         }
         if(enemiesLeft == 0)
         {
             endGame();
         }
     }
     
     void endGame()
     {
         killedAllEnemies = true;
     }
     
     void OnGUI()
     {
         if(killedAllEnemies)
         {
         GUI.Label(new Rect (0,0,200,20),"all gone");
         }
         else
         {
             GUI.Label(new Rect (0,0,200,20),"Enemies Remaining : " + enemiesLeft);
         }
     }
     
 }

something like this maybe.

Comment
Add comment · Show 2 · 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 Reefer · Jul 20, 2013 at 12:13 AM 0
Share

Thank you, just what I was looking for.

avatar image iwaldrop · Jul 20, 2013 at 02:01 AM 0
Share

Using an Update loop with FindGameObjectsWithTag is a processor intensive solution, and will suck up a few ms of frame time. A better way would be to have the enemies that die report that fact to whatever is keeping track of the tally. When the tally reaches 0, you could fire an event so that anything that cares about all of your enemies being dead can react. I typed this code in the comment field, so it's no guaranteed to compile, but give you an idea of what I'm talking about.

 public delegate void AllEnemies$$anonymous$$illed();
 public static event OnAllEnemies$$anonymous$$illed;
 
 public GameObject[] enemyPrefabs;
 private List<Enemy> enemies;
 
 void SpawnEnemies(int count)
 {
     for (int i = 0; i < count; i++)
         enemies.Add(GameObject.Instantiate(enemyPrefab[Random.Range(0, enemyPrefabs.Length));
 }
 
 public void Enemy$$anonymous$$illed(Enemy enemy)
 {
     if (enemies.Contains(enemy))
     {
         enemies.Remove(enemy);
         if (enemies.Count == 0 && OnAllEnemies$$anonymous$$illed != null)
             OnAllEnemies$$anonymous$$illed();
     }
 }

Then, on your Enemy script:

 void OnDestroy()
 {
     GameController.Enemy$$anonymous$$illed(this);
 }
avatar image
0

Answer by fesi · Aug 23, 2017 at 08:29 AM

Array1=GameObject.findGameObjectsWithTag("Enemy"); void Start(){ Array1=GameObject.findGameObjectsWithTag("Enemy"); } Void Update(){ Array1=GameObject.findGameObjectsWithTag("Enemy"); textBox.text=""+array1.length+ "/" + array1.length; } @Reefer ,take another array of game object like totalEnemies and save the enemy tag in it, and then assign it to your text field like this TargetTextBox.text=""+totalEnemies.length; and also assign same enemy tag to enemy game object array in start function. this will give you something like this Enemies: total/remaining.

Comment
Add comment · 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

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

20 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

Related Questions

Enter next level 1 Answer

Load a scene/level when enemy is close... 1 Answer

good resource about conserving memory when multiple levels share resources? 0 Answers

How to restart a "prefab'd" level? ;) 0 Answers

Load Scene by audio clip 2 Answers


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