• 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
Question by Dreave · Sep 19, 2011 at 04:31 PM · enemyhealth

Destroying Enemy Help

I have already got a health script set up for an enemy I have but I don't know how to make the enemy dissapear (die) when its health goes to 0, can anyone help?

Comment

People who like this

0 Show 0
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

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Clunk · Sep 19, 2011 at 04:51 PM

I won't be too specific on script, but I will show u basic idea. Here is if the script is attached to the enemy. If health is less than or equal to 0...

if(health

And here is if script is attached to you, you need to assign the enemy in the inspector (drag and drop) by assigning a variable as Transform. You already know the health part, so I will just put how to destroy on 0. for "gameObject" make sure to use lower case g. The capital G "GameObject" is the component...

var enemy : Transform; var enemy_health : float;

function Update() { if(enemy_health

Comment
clunk47

People who like this

1 Show 27 · 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 Dreave · Sep 19, 2011 at 04:54 PM 0
Share

is your answer in java? cos my health script is in c sharp, does it matter?

avatar image Clunk · Sep 19, 2011 at 05:53 PM 0
Share

yes, this is Java. I don't know Cscript or Csharp. Try it, and if it doesn't work, post up your code and someone who know C will help :)

avatar image syclamoth · Sep 20, 2011 at 01:51 AM 1
Share

To simplifiy, use

 enemy_health -= 10;

it's less confusing.

avatar image syclamoth · Sep 28, 2011 at 04:08 PM 1
Share

At the end of AddjustCurrentHealth() put in a check-

 if(curHealth <= 0)
 {
     GameObject.Destroy(gameObject);
 }

Although, @jfhutchi, @kevork and @Clunk have already told you this! It's not that hard!

avatar image syclamoth · Sep 28, 2011 at 05:11 PM 1
Share

It's always a pleasure to help people kill their enemies.

Show more comments
avatar image

Answer by jfhutchi · Sep 19, 2011 at 05:01 PM

if (enemyHealth == 0) { Destroy(gameobject); }

if your enemy's health is tracked on the Enemy GameObject than that should make it disappear. You could get "fancy" and instantiate a prefab of the enemy exploding(particles) and play a sound there too.

Comment
Rafes
BiG

People who like this

0 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 Rafes · Sep 27, 2011 at 11:00 PM 3
Share

Never check ' == 0'. what if it gets set to -2? Your enemy will never die. Always use '

avatar image

Answer by Clunk · Sep 25, 2011 at 08:56 PM

It's probably where we have GetComponent("Enemy_Health"); That means you need to change "Enemy_Health" to whatever your other script attached to the enemy is named. Since this is attached to the enemy, GetComponent will find any other scripts attached. If your C script you already have is named ehealth, e-health, enemyhealt, etc... Do GetComponent("ScriptName"). Then whatever your health is called inside your script, you do a dot "healthname" so if your script is EnemyHealth, and the health variable inside it is called Health, you would do GetComponent("EnemyHealth").Health = 0. of course, you would use your own names and numbers.

Comment
syclamoth
clunk47

People who like this

0 Show 0 · 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

Answer by kevork · Sep 19, 2011 at 05:02 PM

Assuming the health script is attached to the enemy gameobject, put this in Update() of the health script, and it will remove the enemy gameobject from the scene.

 if(health <= 0)
      Destroy(gameObject);
Comment
Rafes

People who like this

-1 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 Rafes · Oct 12, 2012 at 02:55 PM 0
Share

Waste of Update(), use property getters or an OnHit() event function (as an example)

avatar image

Answer by Sunelol · Oct 12, 2012 at 11:58 AM

How can I make this script work when I have a total of 4 enemies? please help!!

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Health Help 1 Answer

How can I lose health when my enemy collides with the player? 1 Answer

how to kill an enemy 1 Answer

Rect Following Object : Unity3d 1 Answer

Damaging enemies... 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