2
\$\begingroup\$

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?

\$\endgroup\$
  • \$\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\$ – Ruther Rendommeleigh Sep 26 at 11:06
0
\$\begingroup\$

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

GameObject myImage;

void myfunc() {
    myImage.SetActive(false);
}

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

Edit:

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.

Edit2:

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) {
    enemies.Remove(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.

\$\endgroup\$
  • \$\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\$ – Mir Usmanov Apr 12 '18 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\$ – TomTsagk Apr 12 '18 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\$ – Mir Usmanov Apr 12 '18 at 13:01
  • \$\begingroup\$ @MirUsmanov I edited my answer to focus on the enemies \$\endgroup\$ – TomTsagk Apr 12 '18 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\$ – Mir Usmanov Apr 12 '18 at 13:16
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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\$ – Mir Usmanov Apr 13 '18 at 14:20
0
\$\begingroup\$

you can do it in 2 ways.

1) disable gameObject

flagObject.setActive(false);

2) disable Spriterenderer Component

flagObject.GetComponent<SpriteRenderer>().enabled= false; 
\$\endgroup\$
  • 1
    \$\begingroup\$ Please ensure you read the full context around the question. Taking the comment threads into account, just hiding the image was not the OP's problem — they also needed help detecting when all nearby enemies had been defeated. I've edited the question to put this front and center. \$\endgroup\$ – DMGregory Sep 26 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.