Alright as of now Im trying to make it so when I jump on a characters head it destroys the character and all it children. That is working perfectly. The only problem is when I have multiples of these enemys and I land on one of their heads it destroys every enemy that has the script attached. Now here is my code so far.
var children : GameObject[];
function Update ()
{
if (Fallout.enemyisDead)
{
DestroyAll();
}
}
function DestroyAll()
{
for(var child in children)
Destroy(child);
}
That causes every enemy with the script attached to "die" or get destroyed. Please help
You might have a specific reason for destroying the children that way and if you do disregard my advice. But if you Destroy a parent object, all of the children will be destroyed with it. So all you should have to do is (in a script attached to the parent):
function KillThisGuy()
{
//using this will specifically destroy the GameObject attached to this script along with any children.
Destroy(this.gameObject);
}
This method works for me. Hopefully it is what you are looking for.
edit...
///var children : GameObject[];
function Update ()
{
///make sure what ever is controlling this condition is only effecting this local instance
if (Fallout.enemyisDead)
{
DestroyThisGameObject();
}
}
function DestroyThisGameObject()
{
//using this will specifically destroy the GameObject attached to this script along with any children.
Destroy(this.gameObject);
}
create another script and attach it to the enemies head. You can now set a variable in here to determine which enemy you are killing. Like this
var myHeadWasHit : boolean;
function Start()
{
myHeadWasHit = false;
}
Now you can check that variable in your enemyScript
function Update ()
{
///make sure what ever is controlling this condition is only effecting this local instance
if (transform.Find("whatEverYouNamedYourHeadGameObject").GetComponent("nameOfScriptAttachedToHead").myHeadWasHit == true)
{
DestroyThisGameObject();
}
}