I made a script which will subtract 20 hp from the enemy hp when ever my bullet touches it. But instead it was stuck at 80 hp.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public float enemy_hp_1 = 100f;
    public float enemy_hp_2 = 100f;
    public float enemy_hp_3 = 100f;
    public float enemy_hp_4 = 100f;
    public float enemy_hp_5 = 100f;
    public float enemy_hp_6 = 100f;
    public float enemy_hp_7 = 100f;
    public float enemy_hp_8 = 100f;
    public float enemy_hp_9 = 100f;
    public float enemy_hp_10 = 100f;
    public float enemy_hp_11 = 100f;
    public float Kills = 0f;

    void Start()
    {
        
    }
    void Update()
    {
        
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.name == "enemy1")
        {
            enemy_hp_1 = enemy_hp_1 - 20f;
            Debug.Log(enemy_hp_1);
            if (enemy_hp_1 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy2")
        {
            enemy_hp_2 = enemy_hp_2 - 20f;
            Debug.Log(enemy_hp_2);
            if (enemy_hp_2 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy3")
        {
            enemy_hp_3 = enemy_hp_3 - 20f;
            Debug.Log(enemy_hp_3);
            if (enemy_hp_3 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy4")
        {
            enemy_hp_4 = enemy_hp_4 - 20f;
            Debug.Log(enemy_hp_4);
            if (enemy_hp_4 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy5")
        {
            enemy_hp_5 = enemy_hp_5 - 20f;
            Debug.Log(enemy_hp_5);
            if (enemy_hp_5 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy6")
        {
            enemy_hp_6 = enemy_hp_6 - 20f;
            Debug.Log(enemy_hp_6);
            if (enemy_hp_6 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy7")
        {
            enemy_hp_7 = enemy_hp_7 - 20f;
            Debug.Log(enemy_hp_7);
            if (enemy_hp_7 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy8")
        {
            enemy_hp_8 = enemy_hp_8 - 20f;
            Debug.Log(enemy_hp_8);
            if (enemy_hp_8 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy9")
        {
            enemy_hp_9 = enemy_hp_9 - 20f;
            Debug.Log(enemy_hp_9);
            if (enemy_hp_9 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy10")
        {
            enemy_hp_10 = enemy_hp_10 - 20f;
            Debug.Log(enemy_hp_10);
            if (enemy_hp_10 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }
        if (collision.name == "enemy11")
        {
            enemy_hp_11 = enemy_hp_11 - 20f;
            Debug.Log(enemy_hp_11);
            if (enemy_hp_11 <= 0)
            {
                Destroy(collision.gameObject);
                Debug.Log("Enemy distroyed , Fantasctic");
                Kills = Kills + 1;
                Debug.Log("+ 1 kill , Overall Kills = " + Kills);
            }
            Destroy(this.gameObject);
        }


        if (collision.name == "Boundry")
        {
            Destroy(this.gameObject);
        }
    }
}

I would like to help you, but im having trouble understanding what exactly youre doing. i understand youre trying to shoot a bullet at an enemy and have the bullet deal damage to the enemy, however youre doing it kind of backwards. the easiest way to achieve what youre after is to have 4 scripts for this process…Enemy, Gun, Bullet, Scoreboard. Doing it this way you dont have to manually add each enemy and track each enemy in one script like youre doing with the tags and the different enemy hp values. youre making it more difficult for yourself when it could be much easier


NOTE: I have zero experience working in 2D in unity, youll have to convert this to 2D for it to work properly, but it should get you in the right direction.


  • GunScript - Generic script responsibe for instantiating/firing bullets. This means you could have different guns that do different damage per bullet
  • BulletScript - Generic script responsible for holding damage
    information
  • EnemyScript - Responsible for detecting when a bullet hits it and
    managing its own HP
  • ScoreboardScript - Responsible for tracking game stats like kills.

GunScript

public class GunScript: MonoBehaviour
{
    //this script is for shooting the bullets

    public float damagePerBullet = 20f; //damage per bullet
    public float bulletVelocity = 25f; // bullet velocity
    public GameObject bulletPrefab; //reference to the bullet prefab
    public Transform bulletSpawnPoint; // the point where the bullet should spawn (usually the muzzle of the gun)

    //Trigger the bullet to fire. 
    void ShootBullet()
    {
        GameObject bulletObject = (GameObject)Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation); //instantiate your bullet depending on what you need for 2D
        BulletScript bulletScript = bulletObject.GetComponent<BulletScript>();
        bulletScript.Fire(bulletVelocity, damagePerBullet);
    }
}

BulletScript

public class BulletScript: MonoBehaviour
{
    //attach this script to your bullet prefab

    //hidden in inspector because we arent changing this value manually, its being changed by the gun script, public because other scripts access this value
    [HideInInspector]
    public float Damage = 0;
    //reference to the rigidbody so we can change its velocity
    public Rigidbody rb;
    //destroy this object after X seconds (if the bullet ends up hitting nothing just destroy it)
    public float destroyAfterTime = 10f;

    Coroutine bulletRoutine; //not necessarily required, but allows you to reference the coroutine that is waiting to destroy this object

    public void Fire(float velocity, float damage)
    {
        Damage = damage;
        rb.velocity = transform.forward * velocity; //you would have to change transform.forward to whatever direction youre using in 2D
        bulletRoutine = StartCoroutine(DestroyAfterX(destroyAfterTime))
    }

    public void DestroyBullet()
    {
        StopCoroutine(bulletRoutine);
        Destroy(this.gameObject);
    }

    IEnumerator DestroyAfterX(float time)
    {
        yield return new WaitForSeconds(time);
        Destroy(this.gameObject);
    }
}

EnemyScript:

public class EnemyScript: MonoBehaviour
{
    //attach this script to your enemy

    public float maxHealth = 100;
    public float currentHealth = 100;

    private void Start()
    {
        currentHealth = maxHealth;
    }

    void TakeDamage(float amount)
    {
        currentHealth -= amount;
        if(currentHealth <= 0)
        {
            ScoreboardScript.AddKill();
            Destroy(this.gameObject);
        }
    }    

    private void OnTriggerEnter(Collider other) // you would have to change this to 2D
    {
        BulletScript bulletScript = other.gameObject.GetComponent<BulletScript>();
        if(bulletScript == null) { return; }
        TakeDamage(bulletScript.Damage);
        bulletScript.DestroyBullet();    
    }
}

ScoreboardScript:

public class ScoreboardScript: MonoBehaviour
{
    //Add scoreboardscript to a gameobject in your scene and its ready to go.

    public static ScoreboardScript _this;
    public int totalKills = 0;

    private void Awake()
    {
        _this = this;
    }

    public static void AddKill()
    {
        _this.totalKills += 1;
    }
}