Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
Add comment
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
1
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
Add comment · 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
0

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

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
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
-1

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

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
Add comment · 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.

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

Enemy health. Need to take damage from two different bullet prefabs 1 Answer

Increase health smoothly not instantly? 2 Answers

Trap Door Question 1 Answer

How to create Enemy Health Bar ? 4 Answers


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