C# yield waitforseconds

Hey guys,
I started programing a game and i’m currently working on the respawn function using checkpoints. So far everything is going well except one little detail. I’m using a coroutine to make the player wait for X time when he dies and then call the respawn function. The real problem is, everything is executed perfectly until the corutine passes the X seconds wait.

What i want to accomplish is that the player loses control for X seconds and then the 2nd block from the coroutine spawns the player ONCE. I really don’t know why, but after the wait the coroutine keeps executing the 2nd block X seconds.

I would appreciate if anyone could give me a tip on why this is happening, i’m new using the coroutines so i might have fucked it up somewhere.

public void KillPlayer(){
		StartCoroutine (KillPlayerCo ());
	}

	private IEnumerator KillPlayerCo(){
		Seguimentcamara go = GameObject.Find ("Camera").GetComponent<Seguimentcamara> ();
		Player pl = _pc.GetComponent<Player> ();
		pl.Kill ();
		go.isfollowing = false;

		yield return new WaitForSeconds (2f);
		Debug.Log ("WE HAVE WAITED 2S");
		go.isfollowing = true;
		if (_currentCheckpointIndex != -1)
			_checkpoints [_currentCheckpointIndex].SpawnPlayer(_checkpoints[_currentCheckpointIndex]);
		Debug.Log ("END");
	}

I’ve also tried to add a yield break; at the end and it keeps looping the second block.

I’ve done a few tests with the Debug.Log and so far the flow of the code works like this:

public void KillPlayer(){ 
          /*When Killplayer is called it executes this once and then it starts the 
          coroutine.*/
          StartCoroutine (KillPlayerCo ());
     }
 
     private IEnumerator KillPlayerCo(){
         //This block keeps looping until 2s have happened
         yield return new WaitForSeconds (2f);
         //This block keeps looping until 2s have happened
     }

Is this how it should happen? if so how can i code it so it only executes the second part once? I’ve also tried adding a flag inside the second code that calls the respawn function when the flag is true, after executing the respawn it changes to false. The only way to change it back to true is going through the call to start the coroutine. YET it keeps calling the respawn function even with the flag set to false.

PD: Added a bit more info after some tests.

The only explanation is that you’re calling the co-routine multiple times without meaning to. The following code works just fine for me:

using UnityEngine;
using System.Collections;

public class Testing : MonoBehaviour {

	void OnGUI()
	{
		if(GUILayout.Button("Testing"))
		{
			StartCoroutine(test ());
		}
	}

	private IEnumerator test()
	{
		Debug.Log("Start");
		yield return new WaitForSeconds (2f);
		Debug.Log("End");
	}
}

Have a look for when the co-routine is being called, is it when the player collides with something? Health reaches 0 or below? The conditions for calling the respawn code is likely executing more than you’d want it to, as co-routines don’t loop without being told to.