I had a player with three hearts in thee beginning. When he collides one heart is reduced,but the heart is to be recovered after waiting for 5 seconds automatically.
I want if player collides -
with health =3 ( health+=1 after 5 seconds ) ...........................(func1) with health= 2 two times* (health+=1 after 5 seconds) - each going one after another .........................................(func2)
with health =1 ( GAMEOVER )- THIS I HAD ALREADY DONE
I also need, that only one of the above function should run at a time.
Let me explain this - Suppose the player collides when health =3, so function 1 will start.
CASE 1---- However in the meantime, if again the bird collides (health =1) but function 2 must not start until function 1 is done. Although here, in this case, function 2 never starts as after completion of function1 health=2, so function1 will repeat again.
So at all, I want if the collision occurs, health should recover in 5 seconds and recover should not take place when a collision occurs with health=1, as after collision health =0 and GAMEOVER.
Here is the malfunctioned script ---
public class Bird : MonoBehaviour { public GameObject heart1, heart2, heart3; public int health = 3; public float upForce = 200f;
 private bool isDead = false;
 private Rigidbody2D rb2d;
 private Animator anim;
 private bool used = false;
 void Awake()
 {
     heart1.gameObject.SetActive(true);
     heart2.gameObject.SetActive(true);
     heart3.gameObject.SetActive(true);
 }
 // Use this for initialization
 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
      
 }
 // Update is called once per frame
 void Update()
 {
     if (isDead == false)
     {
         if (Input.GetMouseButtonDown(0))
         {
             rb2d.velocity = Vector2.zero;
             rb2d.AddForce(new Vector2(0, upForce));
             anim.SetTrigger("Flap");
         }
     }
     if (health >= 3)
         health = 3;
     switch (health)
     {
         case 3:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(true);
             heart3.gameObject.SetActive(true);
             break;
         case 2:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(true);
             heart3.gameObject.SetActive(false);
             break;
         case 1:
             heart1.gameObject.SetActive(true);
             heart2.gameObject.SetActive(false);
             heart3.gameObject.SetActive(false);
             break;
         case 0:
             heart1.gameObject.SetActive(false);
             heart2.gameObject.SetActive(false);
             heart3.gameObject.SetActive(false);
             break;
     }
 }
 void OnCollisionEnter2D(Collision2D temp)
 {
     if(health>=3)
     {
         health -= 1;
         StartCoroutine(recover());
     }
     if (health == 2)
     {
         health -= 1;
         StartCoroutine(recover2());            
     }
     else if (health <= 1)
     {
         health -= 1;
         rb2d.velocity = Vector2.zero;
         isDead = true;
         anim.SetTrigger("Die");
         GameControl.instance.BirdDied();
     }
 }
 IEnumerator recover()
 {
     yield return new WaitForSeconds(4);
     health += 1;
 }
 IEnumerator recover2()
 {
     yield return new WaitForSeconds(4);
     health += 1;
     yield return new WaitForSeconds(4);
     health += 1;
 }
   
     
 }
 
              Your answer