Triggering only once

I have this fast moving trigger - an arrow.
I want for one arrow to be able to only hit one target, so I delete the Arrow gameObject once the enemy enters it’s trigger. The problem is - if the arrow collides with two enemies in the same time (in one frame), both enemies get their “onTriggerEnter” called, and for neither of them the arrow counts as destroyed.

Any ideas on how I could accomplish triggering only once?

You can use a bool to know if it is the first time that the trigger is hit.

The first time you click ; both receive the onTriggerEnter, but only one will have the bool set as false, so the first object get destroyed, while nothing happens for the second object. When the object is destroyed, the bool is reset (not sure if OnTriggerExit is called when the object is destroyed, but you can reset the bool wherever you destroy the object and the outcome should be the same).

private bool istriggered = false; 

void OnTriggerEnter( Collider coll)
{
    if  (istriggered == false)
        // do your things here that has to happen once
        istriggered = true;
}

void OnTriggerExit( Collider coll)
{
    istriggered  = false;
}

Consider the following code, you have to disable the script and check for it’s state and you’ll be fine.

void OnTriggerEnter(Collider c)
{
    if (this.enabled)
    {
        this.enabled = false;
        //This will fire only the first time this object hits a trigger
    }
}

If anyone interested this kind of works…

void OnTriggerEnter(Collider other) {
	if (other.tag == "Enemy" && hit == 1) {
		StartCoroutine(HitTimer());
	}
}

void OnTriggerExit(Collider other) {
	if (other.tag == "Enemy") {
		hit++;
	}
}

IEnumerator HitTimer() {
	print("Hit Enemy...");
	yield return new WaitForSeconds(hitTime);
	hit = 0;
}

you could try this, so just assign the relevent tags to each gameObject:

note you would assign this to the arrow and as soon as it hit the enemy the first time it would be destroyed, or you could run an animation then destroy it.

void OnCollisionEnter(Collision2D coll) {

if (this.tag == "arrow") {

				if (coll.gameObject.tag == "enemy"){
					Destroy(this.gameObject);
				}

		}
	}