0
\$\begingroup\$

Hey guys need your help with the scripting in Unity, that is, in my game there are several, say, buildings that a main character has to free from enemies and 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. So, 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 be disappered which means that the building was released. Thank you for your attention

\$\endgroup\$
  • \$\begingroup\$ What exactly is the problem? Just make the logo invisible or detecting if all enemies are killed? \$\endgroup\$ – Philipp Apr 12 '18 at 12:55
  • \$\begingroup\$ Detecting the death of enemies in a particular zone and if all of them are dead then make logo invisible \$\endgroup\$ – Mir Usmanov Apr 12 '18 at 13:21
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

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.