Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fierce Waffle · Dec 30, 2010 at 12:36 PM · iphoneenemyjumponhead

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 30, 2010 at 04:06 PM 0
Share

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?)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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();
        }
    }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fierce Waffle · Dec 30, 2010 at 02:44 PM 0
Share

this worked except it destroys every object with the script attached. Not just that one

avatar image s4vi0r · Dec 30, 2010 at 03:07 PM 0
Share

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.

avatar image Fierce Waffle · Dec 30, 2010 at 05:18 PM 0
Share

This is the other code I had

function OnTriggerEnter (other : Collider) { if(other.gameObject.name == "head") { enemyisDead = true; } }

avatar image s4vi0r · Dec 30, 2010 at 05:33 PM 0
Share

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.

avatar image Bunny83 · Dec 30, 2010 at 07:09 PM 0
Share

@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

Show more comments
avatar image
1

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(); } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image s4vi0r · Dec 30, 2010 at 05:52 PM 0
Share

ha. there ya go. nice job.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges