- Home /
How do I make it so my enemy is hit 8 times and is destroyed?
i know there are other posts about this... but i have scoured the internet and have found nothing that solves my problem. i have a bullet prefab that my character shoots with a altered script i used for picking up items and destroying them. basically when the bullet prefab hits the character he is destroyed after one hit. i need it so he can take multiple hits and then destroy. it seems simple enough...but i just cant get it. i have tried for a week now and my brain is melting. here is the script:
#pragma strict
private var score : int = 0;
var counter = 0;
var guiScore : GUIText;
var enemygrunt : AudioClip;
function Start () {
guiScore.text = "KILLS: 0";
}
function OnCollisionEnter(col : Collision){
if(col.collider.name == "ClodHopplerPrefab"){
AudioSource.PlayClipAtPoint(enemygrunt, transform.position);
counter +=1;
if (counter ==2);
Destroy(gameObject);
score += 1;
guiScore.text = "KILLS: " + score;
print("collide with enemy");
}
}
any help would be great. i just cant seem to figure out what to do. i know this is a script for my "bullet" prefab...but do i need to add anything to my enemy to have it work? thanks again. -i'm dying over here.
You might want to format your code using the 101010 button so that your script is readable for other users.
any ideas? it is 6 hours later and i still haven't been able to get this working.
Answer by fafase · Jul 03, 2013 at 06:45 PM
Have a script on the enemy with:
//Health.js
var health :int = 8;
function Damage(damage:int){
health -= damage;
if(health <= 0)Destroy(gameObject);
}
Then on the projectile or whatever hitting the enemy:
//Bullet.js
var damage:int = 1;
function OnCollisionEnter(col:Collision){
if(col.gameObject.tag == "Enemy"){
var script = col.gameObject.GetComponent(Health);
if(script != null)script.Damage(damage);
}
}
in C#
public interface IDamageable{ void Damage(int damage); }
public class HealthController:MonoBehaviour, IDamageable{
int health = 8;
public void Damage(int damage)
{
health -= damage;
if(health <= 0) { Destroy(gameObject); }
}
}
public class Projectile:MonoBehaviour{
[SerializeField] private int damage = 1;
void OnCollisionEnter(Collision col){
if(col.gameObject.CompareTag("Enemy")){
IDamageable script = col.gameObject.GetComponent<IDamageable>();
if(script != null) { script.Damage(this.damage); }
}
}
}
im getting an unknown identifier "health" hmm... i tried making health a variable but i get the same error.
i haven't tried it yet but will in a few $$anonymous$$utes. thank you so much for responding! i have been having so much trouble with this and making animated prefabs. i made a springboard that has a collision trigger on it that cues the animation for the top of the springboard and then a plane without the mesh renderer on to actually lift my character. i cant seem to be able to put the whole thing into a prefab. it doesn't animate anymore.i guess that will be a new topic.
Assets/bulletusewithenemy.js(5,48): BCE0005: $$anonymous$$ identifier: 'Health'. i got this error. i am new to java so be kind. is this because there is no variable "health"?
Did you put the stuff @fafase posted in two different scripts, and name the health script "Health" ?
actually i didn't name it health. good call. i will try that. i am stupid. sorry. i really appreciate the help. i am trying very hard to make this game come together. thanks for all the help.