- Home /
Destroy multiple game objects when player press a button
Hi I am new to C# and Unity. I have searched the web and I am finding it hard to implement a game mechanic. I am working on a mechanic which takes away health to perform a special move. This special move will destroy all enemies with a certain tag, however I am not sure how to do this.
I have came across FindGameObjectsWithTag but as I have said I am not sure how to implement this. I have read the Unity script reference and not sure how to take whats there and put it in my code. If it helps heres the code I have at the moment.
if (Input.GetKeyDown (KeyCode.Keypad8) & shieldHealth > 50)
{
shieldHealth -=50;
print ("I have enough health");
}
Any help is greatly appreciated. Thanks in advance.
Thank you all for your advice. I am trying to use the advice given and I'm not having much luck :(. Like I have said I am new to using C# and Unity. O$$anonymous$$ so heres my code at the moment.
int [] myArray;
void Start () { myArray = new int [3];
myArray [0] = GameObject.FindGameObjectsWithTag ("blueEnemy");
}
and I get this error.
error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to
int'
I'm finding it really hard to understand this. Not just the error code but how to get this mechanic working
I'm trying my best to match the advice given by others below.
I have it working now so for anyone searching this on google heres what i did in C#...
At the top I declared an array.
GameObject [] redEnemies = new GameObject [1];
In the start function I made redEnemies something...
redEnemies = GameObject.FindGameObjectsWithTag ("Red Enemy");
and in the if statement above I killed all gameObjects tagged "Red Enemy" if the player had more than 50 health and pressed the correct button.
THAN$$anonymous$$S TO EVERYONE WHO HELPED!! :)
Answer by Cherno · Sep 26, 2013 at 02:57 PM
var Enemies: GameObject[];//declares an array of GameObjects
//to be filled later
function Start()
{
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
//fill the Enemies array with actual enemy GameObjects
//"Enemy" is the tag your enemies have
}
function Update()
{
if (Input.GetKeyDown (KeyCode.Keypad8) & shieldHealth > 50)
{
shieldHealth -=50;
for(var EnemyCurrent in Enemies)
//iterates (loops) through your array, EnemyCurrent is a temporary
//variable given to the current array entry each loop start
{
Destroy (EnemyCurrent);//Destroys the current enemy object
}
print ("I have enough health");
}
}
Thanks dude! I use C# and will attempt this in my script and will comment on the outcome when its done. Thank you :)
You're welcome. It shouldn't be too hard to convert it to C#, I'm sure you can manage. BTW, robertbu's answer above might be more elegant.
Note that if new enemies can be spawned after Start, you have to re-fill the array. In that case, it's probably best to just put the fill array code bit into the Update function just before the objects get destroyed (after "shieldHealth -=50;").
Answer by Djspun · Sep 26, 2013 at 02:52 PM
this can help you destroy all object with same tag on key press you just need to set a partical affect to go off at the same time to look like an attack
http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy
just giving a suggestion i no theirs a better ways to do it
Thank you for your help! This is great for single objects but not quite what I need. But thank you :)
no problem im still looking for a way to kill off my enemy 1 buy 1 on 0 hp but its a hard one to figure out ill say have a good one
Could you use a timer on a Destroy like Destroy (gameObject, 5), or an if statement that killed your enemies only if their HP hit 0??