Yield WaitForSeconds is not working. Is it my script ?

Basically, if Object A gets collided by Object B, Object A should be invulnerable for 3 seconds before being able to be vulnerable. Invulnerability is a variable attached to Object A. The following is the code to detect collisions and to toggle invulnerability, and is attached to Object B. The Yield WaitForSeconds does not work for some reason. I’m able to Debug upto “before waiting 3 seconds”. What am I doing wrong ?

IEnumerator OnTriggerEnter2D(Collider2D radiusCollisionCheck)
	{
		if (!GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerDamage> ().invulnerable) 
		{
			if (radiusCollisionCheck.gameObject.tag == "Crowd") 
			{
				GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>().DamagePlayer(26);
                GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerDamage>().invulnerable = true;                    // disallow further damage

                Debug.Log ("before waiting 3 secs");
				yield return new WaitForSeconds (3);  // wait for the specified damage timeout
				
				Debug.Log ("waited 3 secs");
				GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerDamage>().invulnerable = false; 
			}
		} else 
		{
			Debug.Log ("Player is invulnerable");
		}	
   	}

The simplest way to limit your coroutine from being called multiple times is to just use a Boolean flag that you set inside the coroutine which will, if it’s set, prevent the coroutine’is body from being executed when set. Then simply unset the Boolean when the initially initiated coroutine is finished, opening it back up for re-entry.

Of course there are more elegant methods of achieving this, but this is probably the best starting point in your understanding of coroutine’is.

Finally, after a lot of hair being pulled out of my scalp, I managed to get this running with some help from Invoke. Here is the code:

private bool coroutineRunning;
	public GameObject radiusObject;

	void Start()
	{
		radiusObject = GameObject.Find("Radius");
		}

	void OnTriggerEnter2D(Collider2D radiusCollisionCheck)
	{
		if (!GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerDamage> ().invulnerable) 
		{
			coroutineRunning = false;
			if (radiusCollisionCheck.gameObject.tag == "Crowd") 
			{
				GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>().DamagePlayer(26);
				
				radiusObject.GetComponent<Transform>().localScale = new Vector3(0.1f,0.1f,0.1f);   // make sure to reset the scale!
				radiusObject.GetComponent<CircleCollider2D>().radius = 3.14f;
				radiusObject.GetComponent<Renderer>().enabled = false;
				radiusObject.GetComponent<Collider2D>().enabled = false;

					if (!coroutineRunning){
					StartCoroutine(Badaboom(2,  0.02f, 0.02f));
					}
			}
		}

		
	}
	void waiting ()
	{
		GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerDamage>().invulnerable = false; 
		Debug.Log ("waited 3 secs");
	}

	IEnumerator Badaboom(int nTimes, float timeOn, float timeOff) //Blink every time.
	{
		while (nTimes > 0){

			GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerDamage>().invulnerable = true;                    // disallow further damage
			Debug.Log ("before waiting 3 secs");
			
			Invoke("waiting",3);
			
			
			GameObject.FindGameObjectWithTag("Player").GetComponent<Renderer>().enabled = false;
			yield return new WaitForSeconds(timeOn);
			GameObject.FindGameObjectWithTag("Player").GetComponent<Renderer>().enabled = true;
			yield return new WaitForSeconds(timeOff);
			nTimes--;
			GameObject.FindGameObjectWithTag("Player").GetComponent<Renderer>().enabled = true;
			coroutineRunning = true;

		}
	}