- Home /
Delete object when two collision or more
Hi i got a script :
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.name == "zombie")
{
Destroy(collision.gameObject);
}
}
Its a nice script, but i want it delete object when two object collide ( its like enemy healt ) like when i shoot two time it delete it, thanks !
Answer by giano574 · Dec 28, 2013 at 07:42 PM
The easiest way would probably be to make a new script for you enemies and attach it to them instead of the bullet. You could do something like this:
var enemyHealth = 2;
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.name == "bullet")
{
enemyHealth -= 1;
if (enemyHealth <= 0)
{
Destroy(this.gameObject);
}
}
}
However, it would probably be a better idea to give your bullets a tag, like "bullet". The code would be almost exactly the same, but instead of collision.gameObject.name it would be:
if (collision.gameObject.tag == "bullet")
Work thanks alot !!! my script var enemyHealt = 2; function OnCollisionEnter(collision : Collision) { if (collision.gameObject.name == "zombie") { enemyHealt -= 1;
if (enemyHealt <=0)
Destroy(collision.gameObject);
}
}
Well, this will work if there is only one zombie in your game. If you kill one zombie and you attack another, the last one will die instantly because "enemyHealt" is below 0.
If you want to have more than one zombie, however, you will have to change the script to look for collisions with bullets and add it to all the zombies (like I showed above). Since there will then be multiple instances of the script, each zombie will have its own health.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Items with Statistics(such as attack damage) that actually effect the character? 2 Answers
gyroscope script problem. 1 Answer
hide object start script 4 Answers