There is a problem with the shieldDestroyed variable. If you declare it with ‘static’ as so, it has same value for all of the Shield instances. So if one shield is destroyed by line 19., every other shield are also inactive having shieldDestroyed equal true.
So I assume your other shields are not Destroy from line 29., they are just inactive. Is it so?
Remove the ‘static’ keyword and it should work.
Edit according to comment:
OK, it is still the same issue. You cannot use static variables - the static makes it same value for all your objects, but you need every object to have its own value. It is mandatory to understand the difference.
I understand why you made it static in first place - you need to access Enemy values from Shield and vice versa. But you need to use reference to the objects, not the class itself. You already have it done right with the Projectile.
So now how to make it work - you have reference to the shield object on line 1 in Enemy, then you make a new instance of the shield on line 10. The ‘shield1’ is exactly you need to use. Assign the ‘shield1’ to field e.g. ‘shieldInstance’ which you declare under line 1. Then everytime you need to check something within the shield, use this variable.
EnemyBehaviour:
public GameObject enemyShield;
private GameObject shieldInstance;
public static float shield;
public float shieldHealth;
private bool shieldDown = false;
void Start(){
Vector3 offsetY = new Vector3 (transform.position.x, transform.position.y - 0.2f);
shieldInstance = (GameObject) Instantiate (enemyShield, offsetY, enemyShield.transform.rotation);
shieldInstance.transform.parent = transform;
shield = shieldHealth;
}
void Update () {
if (shieldInstance.GetComponent<EnemyShield>().shieldDestroyed == true) {
shieldDown = true;
} else {
shieldDown = false;
}
}
void OnTriggerEnter2D (Collider2D col) {
Projectile missile = col.gameObject.GetComponent<Projectile> ();
if (missile != null & shieldDown == true) {
health -= missile.GetDamage ();
missile.Hit ();
}
if (health <= 0) {
Destroy (gameObject);
}
}
Shield:
private Animator anim;
//this variable tells the enemy collider to not collide with projectile if the shield is still up
public bool shieldDestroyed;
private float shieldHealth;
void Start() {
anim = GetComponent<Animator> ();
shieldDestroyed = false;
shieldHealth = EnemyBehaviour.shield;
}
void Update () {
if (shieldHealth <= 0) {
shieldDestroyed = true;
} else {
shieldDestroyed = false;
}
}
void OnTriggerEnter2D (Collider2D col) {
Projectile missile = col.gameObject.GetComponent<Projectile> ();
if (missile != null) {
shieldHealth -= missile.GetDamage ();
missile.Hit ();
anim.Play ("ShieldHit");
if (shieldHealth <= 0) {
shieldDestroyed = true;
anim.Play ("ShieldDown");
Destroy (gameObject, 0.08f);
}
}
}
It will need some performance refactoring obviously, but it is important to understand the concept.