Coroutine being called multiple times from trigger.

Hi peeps :slight_smile:

I’m running the code below. Basically, I want the coroutine to take -10 from currCharLvl.
currCharLvl stores the current charge level of the character, and when full is set to 100. However, when I run the coroutine, the player is left with a currCharLvl of 0.

I’m suspecting that the coroutine is being called multiple times, but not sure how.

Any help is greatly appreciated :slight_smile:

(using C#)

public Collider2D zapAnimation;
public GameObject spawnObject;
public Animator oilPipeAnim;

private bool isActive;

public static float pipeDemand;

void Start(){
	pipeDemand = 10f;
}

void OnTriggerEnter2D (Collider2D other) {

	Debug.Log(other + " collides with oil pipe"); 

	if ((other.gameObject.tag == "zapAnimation") && (ChargePoint.currCharLvl >= pipeDemand) && (zapAnimation.gameObject.activeInHierarchy == true)) { 

		StartCoroutine (PipeDie());

	}
}

public IEnumerator PipeDie(){

	ChargePoint.currCharLvl = (ChargePoint.currCharLvl - pipeDemand);

	yield return new WaitForSeconds (1f);

	oilPipeAnim.SetTrigger ("pipeburst");

	yield return new WaitForSeconds (2.20f);

	Destroy (spawnObject);
	Debug.Log ("Pipe healed!");
}

}

I figured it out just as I posted this. I was calling OnTriggerStay, should have been calling OnTriggerEnter. Hah, a bit awkward… :stuck_out_tongue: