How to avoid getting hit twice with two bullets?

I am quite new to 2d games. In my current project, my enemy fires two bullets:

Instantiate(bullet, new Vector3(transform.position.x - 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity);
Instantiate(bullet, new Vector3(transform.position.x + 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity);

However, when my player gets hit and registers it:

if (name == "Enemy Bullet(Clone)") {
		if (lives > 0) {
				lives--;
				invulTime = 180;
		} else {
				Destroy(gameObject);
				Destroy(other.gameObject);
				Application.Quit();
		}
}

As a result of double shots, my player, if right in the line of fire, loses two lives when getting hit by both bullets. Is it possible to not make that happen? Thank you.

Put a boolean, lets say “hitRegistered” and have it true.
Then you if statement should read

if (lives>0 && hitRegistered){
   hitRegistered=false;
   ...
   WaitOut();
}else{
   ...
}

Since you turned the boolean false, the if statement won’t even start for the second bullet.
in the function WaitOut(), you could put a statement to wait for a second, and then re enable the boolean.

I put it into a separate function because if you put the yield statement in the main if code, it will make the whole thing wait.

I would suggest using cooldowns.

If you have a scipt on your bullet GameObject, you could add an attribute containing the unique identifier of the enemy using GetInstanceId().

private System.Collections.Generic.List<int> RecentShotsIDs;

if (name == "Enemy Bullet(Clone)") {
    int enemyID = collision.GetComponent<Bullet>().enemyID ;
     if( !RecentShotsIDs.Contains( enemyID ) )
     {
         if (lives > 0 ) {
             lives--;
             invulTime = 180;
         } else {
             Destroy(gameObject);
             Destroy(other.gameObject);
             Application.Quit();
         }
     }
     else
     {
         RecentShotsIDs.Add( enemyID  ) ;
         StartCoroutine( RemoveEnemyId( 1, enemyId ) ) ;
     }
}

private IEnumerator RemoveEnemyId( float cooldown, int id )
{
    yield return new WaitForSeconds( cooldown ) ;
    RecentShotsIDs.Remove( id ) ;
}

If you haven’t any script on the bullet and you don’t want to add one, you could add the Instance ID of the enemy in the name of the bullet and try to retrieve it using substring and int.Parse, but not a really good idea.

You could gave an id to the bullets (the same for each set of tzo bullets) and ignore the damage if you gat damage from the same id twice in a row

Or you could use your invulTime.
you probably have a decrement on your “invulTime” so, just check if it is > 0

keep a track of both bullets:

private var bullet_a : GameObject;
private var bullet_b : GameObject;

function InstantiateEm(){
    bullet_a = Instantiate(bullet, new Vector3(transform.position.x - 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity);
    bullet_b = Instantiate(bullet, new Vector3(transform.position.x + 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity);
}

Then I guess you do live remove in some sort of 2D version of OnCollisionEnter (I am not familliar with 2D). So you have to change it somehow like this:

if (other.gameObject == bullet_a || other.gameObject == bullet_b) {
         if (lives > 0) {
                 lives--;
                 invulTime = 180;
                 Destroy(bullet_a);
                 Destroy(bullet_b);
         } else {
                 Destroy(gameObject);
                 Destroy(bullet_a);
                 Destroy(bullet_b);
                 Application.Quit();
         }
 }

If your bullet have a collider, just disable it. Here I am assuming your bullet have a BoxCollider2D

    Instantiate(bullet, new Vector3(transform.position.x - 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity);
    ((GameObject) Instantiate(bullet, new Vector3(transform.position.x + 0.5f, transform.position.y - 1.0f, transform.position.z), Quaternion.identity)).GetComponent<BoxCollider2D>().enabled = false;