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 /
avatar image
0
Question by brokenstars6 · Mar 19, 2019 at 03:05 AM · #pragma

How to make something happen when all of one type of enemies are destroyed by bullets.

I want to have green enemy set a sprite of a key active when all green enemies are dead while having red ones destroyed to do something else. Would I tag all green ones and say something like Update { If (gameobject.tag(“greenenemies” == null ) { Key.setactive;

} And where would I put this code on an empty game object or my character or my key. Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dalsia · Mar 19, 2019 at 03:40 AM

Based on what you mentioned in your question, a simple way to do this would be to check your scene for enemies with certain tags when an enemy is killed; there isn't a need to check every object in the scene each frame with Update(). Something like this:

 //when an enemy's health is <= 0
 if (GameObject.FindGameObjectsWithTag("green").Length == 0)
 {
     //do something
 }

 else if (GameObject.FindGameObjectsWithTag("red").Length == 0)
 {
     //do something else
 }

There are obviously things you could do to optimize this code, but this is simple and should get you the results you want.

Comment
Add comment · Show 3 · 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 brokenstars6 · Mar 19, 2019 at 11:37 AM 0
Share

Okay thank you would I put this in the start section of my code I’m a bit confused as I’m new to game design. :)

avatar image Bonfire-Boy brokenstars6 · Mar 19, 2019 at 11:55 AM 0
Share

check your scene for enemies with certain tags when an enemy is killed

So no, not in start (unless it's the start of something that happens when an enemy is killed)

avatar image Dalsia brokenstars6 · Mar 19, 2019 at 06:18 PM 0
Share

You didn't post any code, so I don't know how you're actually removing health from your enemies. However, you just need to put this check wherever you're doing something like enemyHealth -= 10.

avatar image
0

Answer by coute · Mar 19, 2019 at 12:21 PM

You could have an enemy spawner increment a counter if you have a variable amount of enemies or enemies that are constantly spawning in. If the number stays the same, you could either search for the enemies, drag and drop them into a list and use the list count, or just input the number.

Then on the enemy you could decrement that number in the OnDestroy() method or if you have a list you don't have to do that. Just put in the OnDestroy method a check to see if the number / list count is <= 0.

If it is, you do whatever you want to do when they're all dead.

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

102 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Access GameObject.collider.cente in #pragma strict 1 Answer

GameObjects turn invisible when i teleport the Player's transform to a respawn point. 0 Answers

Should I get checked out? 0 Answers

Mic recording with AR foundation causes app to freeze. Need help please 1 Answer

what are #pragma? 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