- Home /
 
 
               Question by 
               DubstepDragon · Jan 03, 2014 at 10:03 PM · 
                health  
              
 
              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! :D
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by HappyMoo · Jan 03, 2014 at 10:12 PM
 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!!!
Your answer
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
Ragdoll after healths gone 0 Answers
Increase health smoothly not instantly? 2 Answers
Healthbar above enemy : 2d 2 Answers
Destroy(this) not working properly 2 Answers