- Home /
Enemy Health Damage
So i have a game where enemy's spawn and walk towards you and you have to shoot them, only problem is all enemy's in the scene have there health going back upto 100 when another enemy is instantiated the scripts look like this.
function Start () {
CurrentHealth = FullHealth;
}
function Update () {
Debug.Log(CurrentHealth);
if(CurrentHealth < 0){
CurrentHealth = 0;
}
if(CurrentHealth == 0){
Destroy(gameObject);
}
}
function DamageHealth(damage : int){
CurrentHealth -= damage;
}
and on the player i have this to damage the zombie's
var DamageScript: ZombieHealth = GameObject.FindWithTag("Zombie").GetComponent(ZombieHealth);
now i know what the problem is, its because all the zombies when instantiated have the same script and tag but my question is how do i solve this? will i have to have different scripts for each zombie because im planning to instantiate loads at a time.
How are you instantiating the zombies? When you deal damage are you just pressing a button that will deal damage to all of them?
no im shooting at them with a gun, i also am instantiating the zombies on a timer so every 5 seconds one spawns.
Answer by aldonaletto · Sep 23, 2012 at 12:27 AM
That's not the cause of your problem: I suspect you've declared CurrentHealth static, what makes it common to all zombies. If so, remove the static keyword - this way there will be a local CurrentHealth for each zombie.
But the way you're applying damage is wrong too, as you've suspected. You must somehow get a reference to the zombie you've shot in order to apply damage to the right monster. If you're shooting with Raycast, get the reference from the RaycastHit structure, like this:
var hit: RaycastHit;
if (Physics.Raycast(..., hit)){
// something was hit - try to get the ZombieHealth script:
var dmgScript: ZombieHealth = hit.transform.GetComponent(ZombieHealth);
// if the victim has such script, apply damage 10:
if (dmgScript) dmgScript.DamageHealth(10);
}
If you're hitting the zombie with a projectile, get the reference in OnCollisionEnter (in the bullet script):
function OnCollisionEnter(col: Collision){
// try to get the ZombieHealth script:
var dmgScript: ZombieHealth = col.transform.GetComponent(ZombieHealth);
// if the victim has such script, apply damage 10:
if (dmgScript) dmgScript.DamageHealth(10);
Destroy(gameObject); // destroy the bullet
}
Ok, I'm the type of person that wants to know why everything works. This fixed my problem, but why doesn't it work the other way. There is a script there, so why doesn't it complete the function with out the extra if statement? Thanks for the help!
This was of great help for me too. Thank you very much :)
@CG-DJ: the extra if checks whether the object hit has the script ZombieHealth, and if so calls its function DamageHealth. Without this if that line would be just dmgScript.DamageHealth(10);, but you would get Null Reference errors if the projectile hit a wall, the ground or any object that doesn't have the script DamageHealth.
Answer by kookabara · Nov 13, 2013 at 10:59 AM
bullets need a multiple tagging from different element and different prefab effects from what i experience. i instantiate multiple items on their respective tags so i know what im hitting and this really prevent the triggering objects to activate at real time.