Get new GameObject after Destroyed.

I have made a script there I make damage to enemy, but when I kill one enemy I getting “Missing Object”.

Do I need to loop instead?

My code it’s:

public class playerDamage : MonoBehaviour
{

public int melee = 25;
public GameObject player;
public GameObject enemy;
private enemyHealth enemyHealthScript;
private float dist;

void Awake() 
{
	player = GameObject.Find ("Player");
	enemy = GameObject.FindGameObjectWithTag ("Enemy");
	enemyHealthScript = enemy.GetComponent<enemyHealth> ();
}

void Start() {

}

void Update() 
{
	
	MeleeDamage ();

}

void MeleeDamage() 
{

	if (Input.GetKeyDown (KeyCode.Alpha1)) 
	{
		if (player) 
		{
			dist = Vector3.Distance (player.transform.position, enemy.transform.position);
			if (dist < 2) 
			{

				enemyHealthScript.eHealth -= melee;

			}
		}

	}
}

}

Right now you get a reference to one enemy in Awake(). If you destroy that enemy, you can’t ask for enemy.transform.position because the enemy doesn’t exist anymore. You’ll get a missing reference exception.

I don’t know how you want your game to work and i don’t know how many enemies you got or are you creating them on the fly, but you have to look for a new enemy after you destroy the old one.

up to what i understood void OnDestroy() method 'll work for you.
this method will call after the object(for which you attached the script) getting destroy.
in this method write your code to instantiate new one