How do i destroy one enemy if they all have one tag, all spawn from a prefab , and there are a lot that spawn. C#

using UnityEngine;
using System.Collections;

public class PlayerKiller : MonoBehaviour
{
private CoinCounter coinCounter
{
get
{
return GetComponent();
}
}

 public GameObject enemykiller;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

}

//FIX THIS
void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Enemy"))
    {
        if (coinCounter.count >= 2)
        {
            Destroy(enemykiller);
            coinCounter.count = coinCounter.count - 2;
        }

        if (coinCounter.count < 2)
        {
            Application.LoadLevel(Application.loadedLevel);
        }

    }

}

}//end script

First remove public GameObject enemykiller;, because you don’t need it.

When you use OnTriggerEnter2D(Collider2D other){} it gives you access to the Collider2D of the object(enemy object in your case) you are colliding with currently. So you need to destroy the gameObject attached to this Collider2D.

Here’s how you do it:


void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Enemy")){
        Destroy(other.gameObject);
    }
}