My enemies' health wont go down
So I have an enemy prefab that gets randomly spawned as the player progresses through the area. The player has a weapon circle collider (which is a trigger) that gets activated when space is hit, and I check in the enemy ai script if the enemy is colliding with that weapon collider, then subtract the health by the damage. In update, I check if the enemy health is less than or equal to zero, then destroy that instance of the prefab. The problem is, the enemy health isn't changing when I look in the inspector, yet I know that it's colliding with the weapon. Am I doing this right?
#pragma strict
var player : GameObject;
var enemy : GameObject;
var STATCONTROL : GameObject;
var damage : int;
var enemyHealth : int;
function Start () {
damage = -1;
enemyHealth = 1;
player = GameObject.FindWithTag("Player");
}
function Update () {
Debug.Log(enemyHealth);
//
// if(this.transform.position.x == player.transform.position.x){
//
// }
// if(this.transform.position.x > player.transform.position.x){
// this.gameObject.GetComponent.<Rigidbody2D>().velocity.x = -2;
//
// }
// if(this.transform.position.x < player.transform.position.x){
// this.gameObject.GetComponent.<Rigidbody2D>().velocity.x = 2;
//
// }
// if(this.transform.position.y > player.transform.position.y){
// this.gameObject.GetComponent.<Rigidbody2D>().velocity.y = -2;
// }
// if(this.transform.position.y < player.transform.position.y){
// this.gameObject.GetComponent.<Rigidbody2D>().velocity.y = 2;
// }
if(enemyHealth <= 0){
Debug.Log("i have perished");
Destroy(enemy.gameObject);
}
}
function OnTriggerEnter2D(col : Collider2D){
if(col.gameObject.tag == "Weapon"){
Debug.Log("weapon hit");
enemyHealth = enemyHealth - STATCONTROL.gameObject.GetComponent.<MainCharStats>().totaldamage;
}
}
Your answer
Follow this Question
Related Questions
How To Destroy/Deactivate one enemy without affecting other enemies of the same type? 0 Answers
AI targeting player in complete wrong direction 1 Answer
How to set and change enemy path in real time in game ? 1 Answer
Pushing enemy makes them stop following the player 0 Answers
Enemy jump to player position then stop? 0 Answers