Enemy Damage Script?

public int EnemyHealth = 100;
public int bulletDamage = 33;
public GameObject Bullet;
public GameObject enemySelf;

void Update () {
       if (//"Bullet" is 0 away from "enemySelf") {
                 //subtract "bulletDamage" from "EnemyHealth"; 
                 Destroy(enemySelf); 

       }
}
1 Like

Use either OnCollisionEnter or OnTriggerEnter if you’re firing a physical bullet. Alternatively if you don’t want to fire a physical bullet use raycasting.

dude ,do it nice and simple.create an EnemyHealthManager.then create a script for bullets .
so when the bullet hit your character it access to your EnemyHealthManager and reduce health of your enemy.

it’s enemy healthManager script.attach it to your enemy.

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

        public class EnemyHealthManager : MonoBehaviour {

public int HP;

            public void DamageToEnemy(int dmg){
           HP-=dmg;
           if(HP<=0){
           //KILL enemy;
                    }

	}

}

and it’s the bullet script.attach it to your bullet .

                public class bullet : MonoBehaviour {

public int DamageToEnemy;
     
                 void OnTriggerEnter2D(Collider2D other){
	if (other.tag == "Enemy") {
		other.GetComponent<EnemyHealthManager> ().DamageToEnemy(DamageToEnemy);
		Destroy (gameObject);

	}
}

}

and also remember that you should add boxCollider2D component to your enemy and bullet.and your bullet boxcollider2d should be trigger. and your enemy should have RigidBody2D component.and also you should create a tag named Enemy.and make tag of your enemy object to Enemy.

hope it helps.