// Called everytime our object collides with a trigger collider void OnTriggerEnter2D(Collider2D collider) { // We try to identify the object that collided with us as a projectile (laser beam). Projectile missile = collider.gameObject.GetComponent <Projectile>(); AsteroidController asteroid = collider.gameObject.GetComponent <AsteroidController>(); // If our ship collided with a laser beam, we decrease our ship's health in the amount // of damage set by the projectile. If the ship's health is zero or less, then we destroy // our ship. if (missile) { health -= missile.getDamage(); missile.Hit(); // The missile is destroyed upon collision with our ship. if (health <= 0) { Die(); } } else if (asteroid) { health -= asteroid.getDamage(); asteroid.Crash(); if (health <= 0) { Die(); } } hpText.text = "HP : " + health; }