- Home /
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.
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.
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. :)
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)
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
.
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.
Your answer
Follow this Question
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