Kill Clones

Hello People!
im trying to kill Clones with a Button that i Spawn with a Script. They receive the name Enemy(Clone)

i tried both ways…

public GameObject Enemy;
public void Matar()

Destroy(GameObject.Find(Enemy + “(Clone)”));
//Destroy(Enemy.gameObject);

thanks!!! im so noob!

A little more information, please?
Do you want to kill just 1 of them or all of them?

Will you be “killing” the clone from the same script that spawned it? :slight_smile:

This line:

Destroy(GameObject.Find(Enemy + "(Clone)"));

Might not do what you want.
Try:

Destroy(GameObject.Find(Enemy.gameObject.name + "(Clone)"));

In future make sure to try and obtain better debug information like so:

string enemyToFind = Enemy.gameObject.name + "(Clone)";
Debug.Log("Searching for enemy: " + enemyToFind);

GameObject enemyToKill = GameObject.Find(enemyToFind);
Debug.Log("Enemy found: " + (enemyToKill != null));

Destroy(enemyToKill);

I want to kill all enemys. But they spawn every 5 seconds

Thanks!!! i will try it and lets see if it Works!!! :slight_smile:

I would say that there are a couple of good options.

  1. add each enemy you spawn to a list, and when you want to destroy them all with your button, go over the list and destroy each.
  2. less good than the first option, imo, but you could use FindObjectsOfType (assuming enemies share a common script that other game objects do not have)
    2b) or FindObjectsWithTag.
1 Like

Thank u for ur reply!Can u write please how i do that with the list that i can search it in the documentaion of Unity??
Thanks a lot!!!

Unclear as to which part you’re asking about.
I’m suggest that you create a list of type game object
Now, when you spawn an enemy, add it to this list (probably from the spawn script, would make the most sense).
Then, when you delete, go over the list and Destroy the game object.
Note: Destroy(game object) is a unity method.

Do you know how to make a list? Go over it? Add to it?
Lists are part of C#, and used in Unity… they are not a Unity only thing.

Got it! i will check Lists in C# and apply it to my game!

thanks!

No problem :slight_smile: Hope you get it working. Take care.

1 Like