Question by
EvyBenita · Jul 07, 2016 at 07:28 AM ·
coroutinetriggersontriggerstayontriggerenter2d
Coroutine being called multiple times from trigger.
Hi peeps :)
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 :)
(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!");
}
}
Comment
Best Answer
Answer by EvyBenita · Jul 07, 2016 at 07:28 AM
I figured it out just as I posted this. I was calling OnTriggerStay, should have been calling OnTriggerEnter. Hah, a bit awkward... :P