- Home /
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?
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
is your answer in java? cos my health script is in c sharp, does it matter?
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 :)
To simplifiy, use
enemy_health -= 10;
it's less confusing.
It's always a pleasure to help people kill their enemies.
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.
Never check ' == 0'. what if it gets set to -2? Your enemy will never die. Always use '
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.
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);
Waste of Update(), use property getters or an OnHit() event function (as an example)
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!!
Your answer
Follow this Question
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