I'm making a shield system for the enemy ships. I have all of the shields running off of the same shield script, which seems to work fine other than when it destroys the game object. All of the ships take getting the shield hit fine and plays corresponding animation for the shield hit and decrements the shield health separately from each other but when i destroy one of the shields, all of the shields get destroyed. It could be a simple mistake because I'm a noob at coding and Unity but any help would be so greatly appreciated.
Here is the code that runs the shields:
public float shield = 100f;
private Animator anim;
//this variable tells the enemy collider to not collide with projectile if the shield is still up
public static bool shieldDestroyed;
void Start() {
anim = GetComponent ();
shieldDestroyed = false;
}
void OnTriggerEnter2D (Collider2D col) {
Projectile missile = col.gameObject.GetComponent ();
if (missile != null) {
shield -= missile.GetDamage ();
missile.Hit ();
anim.Play ("ShieldHit");
if (shield <= 0) {
shieldDestroyed = true;
StartCoroutine(Shield());
}
}
}
IEnumerator Shield () {
if (shield <= 0) {
anim.Play ("ShieldDown");
yield return new WaitForSeconds (0.06f);
Destroy (gameObject);
}
}
↧