Unity – How to destroy / disable an enemy without touching other enemies of the same type?

Your problem is these two lines:

zombieController = FindObjectOfType();

That said, give me the first ZombieController you find in the scene "

zombieController.Stomped ();

That said, squash this specific zombie you have found for me earlier "- that is, it does not matter if it's the zombie you actually landed or if that zombie is still alive.You set up your script so that only one zombie is predestined to receive every trample

To avoid this, I recommend you to centralize the work of handling to be crushed on the object that can be overwritten. In this way, any crush always applies to "me", which is trivial to ensure that it reaches the right target.

Presumably, you will want other things that you can crush beyond zombies (for example, other types of monsters, bonuses, maybe even the player himself (falling blocks / etc), then let's separate the concept of Squashable of the concept of Zombie while we are there.

First, we will use a very simple script to mark our crashing collider. You can also do this with tags, but doing it with a script allows you to attach parameters to it (for example, a damage value inflicted by that squasher or flags for selective squash immunity, so you ensure that players do not reduce other players)

public class Squasher: MonoBehaviour {}

Then we will define our script to be overwritten. I've configured this in a generic way so that it can work on anything that has squash animation. This reduces code duplication if you add other compressible types later.

public class Squashable: MonoBehaviour {

bool squashed = false;

void OnTriggerEnter2D (Collider2D other) {
// Do not double if you have two triggers, enter messages at once.
if (overwritten)

// only squash if we are hit by a collider.
// (can check that it also comes from above)
var squasher = other.GetComponent();
if (squasher == null)

StartCoroutine (SquashMe ());

IEnumerator SquashMe () {
crushed = true;

var animator = GetComponent();
if (host! = null)
animator.SetBool ("Squash", true);

var body = GetComponent();
if (body! = null)
body = new Vector2 (Mathf.Epsilon * Mathf.Sign (transform.localScale.x), 0f);

// Have your Zombie Controller or other AI Monster implement this interface,
// so we can disable their behavior / attribution score or pickups as needed,
// without Squash logic knowing anything about "zombies".
killable var = GetComponent();
if (killable! = null)
killable.Kill ();

int oldLayer = gameObject.layer;
gameObject.layer = 11;

return return new WaitForSeconds (2f);

// Hide this object and replace all its components in the sense that we found them.
gameObject.SetActive (false);
gameObject.layer = oldLayer;
if (host! = null)
animator.SetBool ("Squash", false);
if (killable! = null)
killable.Resurrect ();
crushed = false;