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 spiralfire11 · May 07, 2013 at 11:40 PM · enemyloadlevel

Im trying to make a Java script that detects how many tags of Enemy are still alive. How exactly do i do that?

I have a tag named enemy. The script is supposed to detect how many enemy tags are alive and if there are none, the game loads the next scene.

var Enemy = tag { if (Enemy = 0) {

Application.Loadlevel("win");

} }

Comment
Add comment · Show 4
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 Crazydadz · May 07, 2013 at 11:59 PM 0
Share

Im not in front of my cpu but you can have a variabl enemy count and each time an enemy die do enemycount --; if enemycount == 0 load new level. You can also use event to achieve this

avatar image robertbu · May 08, 2013 at 12:08 AM 0
Share

GameObject.FindGameObjectsWithTag()

avatar image Crazydadz · May 08, 2013 at 12:29 AM 0
Share

If you use GameObject.FindObjectsWithTag(), youll need to call this function each time an enemy die and it will take a lot of "power" for nothing

avatar image robertbu · May 08, 2013 at 02:00 AM 0
Share

@Crazydadz - It is my understanding (and my experience) that, unlike GameObject.Find(), FindObjectsWithTag() is efficient enough that it can be called every frame. This is especially true if the number of tagged objects is small. But in the case above, it doesn't need to be called every frame. Probably four times a second would work just fine.

Your solution is unarguably more efficient, but it comes with its own set of issues and complexity. You have to figure out how to communicate the creation and kills of enemies to wherever the code above lives. So we are talking about a Singleton, or a static variable (with its own set of issues), or C# events or taking the "death" away from the individual enemy and handling it somewhere central. All involved more complexity than I though was helpfulto the OP based on the code posted.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ice Koobs · May 08, 2013 at 02:34 AM

You could use the following:

 var enemiesLeft:int;

 function Update()
 {
     for(var badGuys : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
         {//Find anything with the tag Enemy
             enemiesLeft++;
             //Every time you find an enemy add it to a the total number of enemies
         }
             
     Debug.Log(enemiesLeft);
     //Use Debug.Log to keep a track of numbers while testing and debuging.
     //Delete once you know the code is sound
 
     if (enemiesLeft==0)
     {
                 Application.Loadlevel("win");
     }
 }
         

But Crazydadz is correct. This will take up a bit of juice.

You're better doing something along the lines of the following Have a script called "LevelControl"(this is important for later):

 static var enemiesLeft[:int;
 
 function Start()
 {//This will happen once at the beginning of each level just to find total number
 for(var badGuys : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
     {
     enemiesLeft++;
     }
 }

 function Update()
 {
 if (enemiesLeft==0)
     {
      Application.Loadlevel("win");
     }
 }


Then have a script attached to the enemy that when they're destroyed the following line is actioned.

 LevelControl.enemiesLeft--;
 //When dead take one away from the enemiesLeft variable in the script LevelControl


Hope that solves your problem.

Koobs

Comment
Add comment · Show 8 · 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 robertbu · May 08, 2013 at 02:36 AM 0
Share
 if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
     Application.Loadlevel("win");
avatar image spiralfire11 · May 08, 2013 at 08:07 PM 0
Share

Thanks! But the LevelControl thing says this compiler error: An instance of type 'LevelControl' is required to access non static member 'enemiesLeft'.

avatar image Ice Koobs · May 08, 2013 at 10:16 PM 0
Share

$$anonymous$$y mistake. I've corrected the script. The variable should have 'static' not 'public'.

avatar image spiralfire11 · May 09, 2013 at 01:54 AM 0
Share

Everything works, but when I load the first level, the function update goes to fast and loads 'win' already. But when i go on the second level and die, and start back at the first, the function update does nothing and I can play. But when all the Enemies are killed, it doesn't load 'win'. But everything else works, so thank you in advance.

avatar image Ice Koobs · May 09, 2013 at 01:56 AM 0
Share

It's set off before you even killed the enemies or as soon as the level starts?

Show more comments
avatar image
0

Answer by Loius · May 09, 2013 at 06:57 PM

What I do with pretty much all my instantiable classes now is this:

 using System.Collections.Generic;
 
 public class Mine : MonoBehaviour {
   public static List<Mine> instances = new List<Mine>();
 
   void OnEnable() {
     instances.Add(this);
   }
   void OnDisable() {
     instances.Remove(this);
   }
 }
 
 ...
 
 Debug.Log("There are currently " + Mine.instances.Count + " mines");
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

15 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

Related Questions

Loading new level after killing enemies 3 Answers

How do you create a "collect" objection that corresponds with the enemy behavior in C#? 1 Answer

How to stop enemy shooting through wall 1 Answer

Model Problem pls help. 0 Answers

force traslation 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