- Home /
Scripts Destroys all Objects its Attached to!
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
Thank you in advance.
I guess "enemyisDead" is a static bool variable in a class called Fallout. You need a different method to deter$$anonymous$$e whether an enemy is dead or not. A static variable exists only one time in your whole game, so when it turns true every enemy sees that in its Update and kill itself. Like s4vi0r said, you can use a local (nonstatic) variable or the direct way by calling DestroyAll of a particular enemy at the moment of "killing/hitting".
$$anonymous$$ore code would be useful, esp. the part where you deter$$anonymous$$e if you hit an enemy (Fallout class?)
Answer by s4vi0r · Dec 30, 2010 at 02:40 PM
Hey Mr. Waffle,
=D
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 back to your trigger.
OnTriggerEnter (other : Collider) {
if(other.gameObject.name == "head")
{
other.GetComponent("nameOfScriptAttachedToHead").myHeadWasHit = true;
}
}
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();
}
}
this worked except it destroys every object with the script attached. Not just that one
in your if statement. make sure that where ever you are setting that value that deter$$anonymous$$es if the object is destroyed is only effecting that instance you want destroyed.
without seeing more code I can't be certain whats going on.
you might want to add a health variable. Or some local variable you can check to deter$$anonymous$$e when to destroy it. Or maybe OnCollision() or something.
This is the other code I had
function OnTriggerEnter (other : Collider) { if(other.gameObject.name == "head") { enemyisDead = true; } }
This is what I think is happening. When you set that variable in that script (I'm assu$$anonymous$$g its your player script) all of those enemies read it from their own scripts since you are calling it in the Update(). So what you need to do is designate which enemy is dead. I'm am going to edit my post with a suggestion.
@s4vi0r: I just want to add that calling transform.Find() and GetComponent() with string arguments every frame is evil! it's better to keep a local variable to hold a reference to your script-component. in your start method you setup your reference: $$anonymous$$yScript = transform.Find("whatEverYouNamedYourHeadGameObject").GetComponent("nameOfScriptAttachedToHead");
than you can use $$anonymous$$yScript.myHeadWasHit
Answer by Fierce Waffle · Dec 30, 2010 at 05:51 PM
For anyone wondering this is what I cam up to be my final solution
var enemyisDead;
function DestroyThisGameObject() { Destroy(this.gameObject); }
function OnTriggerEnter (other : Collider) { if(other.gameObject.name == "Player") { DestroyThisGameObject(); } }
Your answer
Follow this Question
Related Questions
Jump on Enemy heads to destroy enemy? 1 Answer
Enemies spawn on top of each other 2 Answers
Detect Hit on top of collider(head) 2 Answers
On Collide Destroy Game Object 2 Answers
Accelerometer question 1 Answer