I’m making a tower defense game. The problem I’m having is that whenever a bullet hits, all the enemies lose hp instead of just the one collided with by the bullet. The first mob in each wave is normal, but when it is dead, every mob after him has 0 hp or less and will be killed as soon as a bullet hits them.
Is there a way to access the static variable in the enemy collider without accessing the script. (I’m guessing the problem is that it’s accessing the script and reducing hp by every gameObject that has that type.)
This is the function in bullet script:
var bulletSpeed : int;
var lifeTime : float;
var damage : int;
function Start () {
lifeTime = Time.time;
}
function Update () {
transform.Translate(Vector3(0,0,bulletSpeed));
if (lifeTime + 0.5f < Time.time)
{
Destroy(this.gameObject);
LifeLostScript.enemies--;
}
}
function OnTriggerEnter (collider : Collider) {
if (collider.tag == "Enemy")
{
collider.GetComponent(EnemyMovementScript).hp -= damage;
if(collider.GetComponent(EnemyMovementScript).hp <= 0)
{
Destroy(collider.gameObject);
}
Destroy(this.gameObject);
}
}
This is the enemyscript:
var speed : int;
var HP : int;
static var hp : int;
function Start () {
hp = HP;
}
function Update () {
transform.Translate(Vector3(speed*Time.deltaTime, 0, 0));
}
function OnTriggerEnter (collider : Collider){
if (collider.tag == "RightRotation") {
transform.Rotate(0,90,0);
}
if (collider.tag == "LeftRotation") {
transform.Rotate(0,-90,0);
}
}