unity – How to destroy / deactivate an enemy without affecting other enemies of the same type?

Your problem are these two lines:

zombieController = FindObjectOfType();

This says "give me first ZombieController you find it in the scene "

zombieController.Stomped ();

This says "squash that specific zombie you found it before, "that is, it does not matter if that is the zombie you really fell into, or if this zombie is still alive. You have set up your script so that one and only one zombie is predestined to receive each step very strongly.

To avoid this, I recommend centralizing the management work. being crushed About the object that can be crushed. In that way, any crushing always applies to "I," which makes it trivial to ensure that it hits the right target.

You will probably want other things that you can crush beyond the zombies (for example, other types of monsters, special powers, maybe even the player himself (from falling blocks / etc), so let's separate the concept of Overwhelming from the concept of Zombie while we are in it.

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

Public class squasher: MonoBehaviour {}

Next we will define our script to be crushed. I have configured this in a generic way so that it can work on anything that has a squash animation, so that it minimizes the duplication of code if it adds other squashing types later on.

Public class Squashable: MonoBehaviour {

crushed bool = false;

void OnTriggerEnter2D (another Collider2D) {
// Do not double-squash if you get two input messages at the same time.
yes (crushed)
he came back;

// Just crush if hit by a squasher collider.
// (You can check that it came from above too)
var squasher = other.GetComponent();
if (squasher == null)
he came back;

StartCoroutine (SquashMe ());

IEnumerator SquashMe () {
crushed = true;

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

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

// Make your Zombie controller or another AI monster implement this interface,
// so we can disable your reward behavior / score or collections as needed,
// without the Squash logic knowing anything about "zombies".
var killable = GetComponent();
if (killable! = null)
killable.Kill ();

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

yield return new WaitForSeconds (2f);

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