- Home /
A trigger to kill all parts of an enemy?
This is a general question, but I have multiple box collisions and triggers on my enemies in my game. My character has a power up that makes her kill anything she touches, but sometimes not ALL of the parts of the enemy are taken out (such as the moving script or others{I have enemies you can bounce on and stuff}).
I was wondering if there was a way for me to just make a trigger and say, "EVERYTHING that is a child of this.... if the player has the powerup and hits this trigger... kill everything... KILL ALL THE CHILDREN!!"
....is there a script that allows me to kill all the children? If you need more information from me, let me know and I'll tell you more things in depth. Thanks a lot for your help!
Do you have an array or list type container where you are storing the number's of instances of Enemy "GameObjects" ?
no, im unsure how to use arrays. I just have some in empty game objects, and others contained under the graphics part of the enemy. It's dependent on what kind of enemy it is, and what needs to allow the player to survive, when touched. So everything is set up as a parent with children, to keep the prefabs working. Nothing is done through an array, mostly because, again, I have no experience with that kind of thing.
Answer by Crazydadz · Nov 06, 2013 at 11:30 PM
Do some search before posting question. They are a lot of way to destroy childrens. One of multiple answer
be more specific in your question though...Find a way to transpose it, its easy though. $$anonymous$$arkD did it! You are lucky.
Answer by MarkD · Nov 06, 2013 at 11:33 PM
Well, if you want the colliders to be 'gone' from the game world and never come back, you already gave your answers yourself you simply search all the children and destroy them.
var parent:Transform;
Java
for (var:Child in parent){
Destroy(Child.gameObject);
}
C#
Transform parent;
foreach(Transform child in parent) {
Destroy(Child.gameObject);
}
Now note that this is made on the fly and has not been tested, but it gives you a general starting point to go from.
Now if you want only to delete the scripts of the enemy but keeps all its GameObjects alive in the game you should take a look at this http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html
thanks a lot! I will toy with that in a bit. I wasn't familiar with the child.gamObject script so i didn't know how to search for solutions. It's been driving me nuts! lol. thanks.
am i allowed to attach a script like this to an empty game object or is that nonsensical?
I'm not getting anything out of your suggestion. it's not working for me. Unity doesn't know what child is..
you have to refer the transform wich contains the childs in the transfrom slot, Unity ofcourse does not know what a child is if it does not get a parent :). and you can atach the script to an empty object, but for that you need to tweak it, toy arround. As I mentioned this script is just a point in the right direction, it is not tested and for that reason might not even work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Kill enemy when animation is playing 1 Answer
Disable a target after trigger exit? 1 Answer