Decrement for a set amount of time...

in my game, the player gets a “poisoned” effect on them. When they do, I want to decrease their HP value by 2 a second, for 12 seconds. However, since I use the Update() method, it ends up at -3950 or something of the sorts…

Here is my code:

using UnityEngine;
using System.Collections;

public class Vitals : MonoBehaviour {

	public int health = 100;
	public int mana = 100;

	public bool poisoned = false;
	public bool dead = false;
	public bool slow = false;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if(poisoned) {
			health--;
		}

		if(health == 0 || health < 0) {
			dead = true;
		}

		Debug.Log ("Player Health: " + health);

	}

	void OnTriggerEnter(Collider other) {
		if (other.gameObject.CompareTag ("Damage")) {
			health -= 5;
		}
		if(other.gameObject.CompareTag("Poison")) {
			poisoned = true;
		}
	}

}

Thanks in advance! :smiley:

if(other.gameObject.CompareTag(“Poison”) & !poisened) {
StartCoroutine(Poisoning())
}

...
...

IEnumerator Poisoning() {
    poisened = true;
    health -= 2;
    for(int i = 0; i<11; i++)
    {
        yield return new WaitForSeconds(1);
        health -= 2;       
    }
    poisened = false;
}

And you’re like: What? Coroutines? OMG!!!