In my game there are several buildings that a main character has to free from enemies. I put a kind of a logo above all those have-to-be-released buildings, indicating that a certain building is not free and it has to be released.

What I want to achieve is that when I kill all of the enemies near a certain building, the logo above that building has to disappear, indicating means that the building was released.

I don't know how to detect this condition of "all nearby enemies defeated" in my script — how can I do that, and hide the image when the condition is met?

  • \$\begingroup\$ Are enemies assigned to a specific building or do they wander between areas? In the second case, can a building be "re-infested" with enemies? Do enemies that leave the area count as "defeated" or should the player have to kill them regardless? \$\endgroup\$ Commented Sep 26, 2019 at 11:06

2 Answers 2


Assuming that your logo is a unity object, the simplest thing you can do is:

GameObject myImage;

void myfunc() {

This will disable your image and all its components, it will also not be visible (since it deactivates the mesh as well).


I forgot to mention, you can also deactivate the image's component, if you want to keep the rest of the object alive:

Sprite myImage;

void myfunc() {
    myImage.enabled = false;

Both examples assume that you know how to get a reference of an object inside a script, if you do not know how let me know and I'll update my answer.

Assuming all your enemies are inside a List, every time an enemy dies, you can remove them from the List (List.RemoveAt(index)). Once the List is empty for a particular building, that means the building is free and you can make the logo disappear.

If your enemies are inside an array where you can't remove them from, add a variable inside enemies like bool isDead = false;, when they die, it becomes true. Once all enemies inside the array have this variable as true it means they are all dead and you can proceed to free the building.


Here's how a script would (ideally) look:

public Transform enemyPrefab;
List<Transform> enemies = new List<Transform>();
int numberOfEnemies = 5;

public Start() {

    // Generate enemies for this building
    for (int i = 0; i < numberOfEnemies; i++) {
        Enemy e = Instantiate(enemyPrefab);
        e.position = new Vector3(i, 0, 0); // Position you want the enemy to have
        enemies.Add(enemy); // Add enemy in the list


public void EnemyDied(Enemy e) {
    if (enemies.Count == 0) {
        // All enemies died, free building

This assumes that you have a prefab of the enemy you want to use. Attach this to each of your buildings, and they will spawn enemies. Once the player attacks an enemy and they die, they should call myBuilding.EnemyDied(this) to announce to the building that they died. (where myBuilding is the building they belong to)

Unfortunately I can't write anything more precise since I don't know what your code looks like so far. If you are struggling with some concepts like what a prefab is, I would suggest looking some unity tutorials online to familiarise yourself with them.

This script is also meant to give you an idea of how this problem could be solved, it's not the only way, and certainly not the best way. Adjust as needed to your situation.

  • \$\begingroup\$ Yes I know this but I have to achieve the disappearance of the logo only after all of the enemies near a particular building were killed \$\endgroup\$ Commented Apr 12, 2018 at 12:56
  • \$\begingroup\$ @MirUsmanov How are you placing the enemies in the game? Are they grouped together, linked on a building, or just roaming around, and happen to approach buildings? \$\endgroup\$ Commented Apr 12, 2018 at 12:57
  • \$\begingroup\$ Well, there are several enemies grouped together( they just stay still in the idle animation) near each building and whenever you approach nearer than 3, say, meters the enemies start coming to you and then attack \$\endgroup\$ Commented Apr 12, 2018 at 13:01
  • \$\begingroup\$ @MirUsmanov I edited my answer to focus on the enemies \$\endgroup\$ Commented Apr 12, 2018 at 13:09
  • \$\begingroup\$ Sorry TomTsagk since I am just at a beginner level in scripting please is there any way for you to show how the code has to look)) \$\endgroup\$ Commented Apr 12, 2018 at 13:16

You can use sphere collider (or something like this, can't say now) to detect enemies in specified area. When collider won't be able to find anybody it can be understood as "building is free".

I think first answer (with all edits) can work here better tho, but my solution works too :D Please let me know if you would like to know more about this so I will make an edit with explanations.

  • \$\begingroup\$ Hey TimKah thank you for paying attention to my problem and yes that woluld be great of you to show your solution with explanation:) \$\endgroup\$ Commented Apr 13, 2018 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .