As soon as I press the play button my enemies die instantaneously after I saw a laser on top of it. I checked my Z coordinates many times but I still could not figure out the problem. Here is my code:
public class EnemyBehavior : MonoBehaviour {
public GameObject projectile;
public float projectileSpeed = 10;
public float health = 150;
void Update (){
Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
GameObject missle = Instantiate (projectile, transform.position, Quaternion.identity) as GameObject;
missle.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
}
void OnTriggerEnter2D (Collider2D collider)
{
Projectile missile = collider.gameObject.GetComponent<Projectile>();
if (missile) {
health -= missile.GetDamage ();
missile.Hit();
if ( health <= 0 ) {
Destroy (gameObject);
}
}
}
}