i need help making a code that deystroys object after a number of collisions,i need help with a script to deystroy enemy after a number of times colliding

using UnityEngine;
using System.Collections;

public class damager : MonoBehaviour
{

public int hitsNeeded = 8;
public int hitsTaken;

void OnTriggerEnter(Collider c)
{
    if (c.GetComponent<Collider>().tag == "Enemy")
        hitsTaken += 1;

        Debug.Log("a collision occured, hitsTaken:" + hitsTaken);

    if (hitsTaken >= hitsNeeded)
        Destroy(gameObject);
}

}

Ok, so what's your problem here? What doesn't work? What have you actually tried? This code should work in theory depending on how you setup your scene.

1 Answer

1

Hi! You need such script on Enemy, not on damager.

and your enemy should collide with something not “enemy” I guess. maybe “player”? or “bullet”.