Respawn Script will only instantiate my player once, then never again.

Hi Guys, I’m having an issue with a respawn script i’m trying to write.

The issue is that once ‘playerOne’ has respawned once, if he dies again he doesnt respawn.

The strange thing here is that if ‘playerOne’ is killed and respawns the first time, but then he goes and kills ‘playerTwo’, then ‘playerOne’ dies again, he will respawn for the second time. this can continue to be done as long as ‘playerOne’ kills ‘playerTwo’ after respawning, otherwise he wont respawn for a second time.

heres the code for ‘playerOne’

public class PlayerOne : MonoBehaviour {

	public float speed, jumpForce, stompForce, bounceForce;
	bool grounded = true, isFlipped, landed = false;
	public bool dead = false;
	public Transform groundCheck, groundCheck1, landedCheck, landedCheck2;
	public LayerMask whatIsGround, playerTwoTag;
	

	void FixedUpdate () 
	{
		float move = Input.GetAxis ("PlayerOne_Horizontal");
		//leftRight movement
		rigidbody2D.velocity = new Vector2 (move * speed, rigidbody2D.velocity.y);

		//check if player needs to be flipped, then flip if neccesary
		if (move > 0 && isFlipped)
			flip ();
		else if (move < 0 && !isFlipped)
			flip ();
	}

	void Update ()
	{
		//stomp
		if (Input.GetKeyDown (KeyCode.S) && grounded == false)
						GroundStomp ();

		//jump 
		if (Input.GetKeyDown(KeyCode.W) && grounded == true)
			Jump ();

		//check if grounded
		grounded = Physics2D.OverlapArea (groundCheck.position, groundCheck1.position, whatIsGround);

		//check if player lands on anyone elses head
		landed = Physics2D.OverlapArea (landedCheck.position, landedCheck2.position, playerTwoTag);

		if (landed) 
		{
			StompBounce();
			GameObject.FindWithTag("PlayerTwo").GetComponent<PlayerTwo>().dead = true;
			GameObject.Find ("Score").GetComponent<ScoreScript>().playerOneScore++;
			landed = false;
		}

		if (dead) 
		{
			Death();
		}
	}

	void Jump ()
	{
		rigidbody2D.AddForce(Vector2.up * jumpForce , ForceMode2D.Force);
	}

	void GroundStomp ()
	{
		rigidbody2D.AddForce (-Vector2.up * stompForce, ForceMode2D.Force);
	}

	void StompBounce ()
	{
		rigidbody2D.velocity = new Vector2 (bounceForce, bounceForce);
	}

	void Death ()
	{
		Destroy(gameObject);

	}

	void flip()
	{
		//flips sprite left and right depending which way character is facing
		isFlipped = !isFlipped;
		Vector3 scale = transform.localScale;
		scale.x *= -1;
		transform.localScale = scale;
	}
}

And Heres the code for the respawn script, sorry for the length of the last code block!

public class RespawnScript : MonoBehaviour {

	//constructor
	public Transform[] spawnPoints;
	int spawnNumber;
	public GameObject playerOneSpawn;
	bool playerOneDead;

	void Update()
	{
		if (GameObject.FindGameObjectWithTag ("PlayerOne").GetComponent<PlayerOne> ().dead) 
		{
			StartCoroutine("RespawnPlayerOne");
		}
	}

	IEnumerator RespawnPlayerOne()
	{
		spawnNumber = Random.Range (0, 11);

		yield return new WaitForSeconds (2);
		
		GameObject newPlayerOneSpawn = (GameObject)Instantiate (playerOneSpawn, 
		spawnPoints[spawnNumber].position, spawnPoints[spawnNumber].rotation);
		newPlayerOneSpawn.name = playerOneSpawn.name;
	}
}

Any help will be appreciated guys! This has been doing my head in for days now and I know its probably something im just overlooking. Thanks in advance!

To hazard a guess, You cant Find a game object you have Destroyed!

void Death ()
{
     Destroy(gameObject);
}

void Update()
{
     if (GameObject.FindGameObjectWithTag ("PlayerOne").GetComponent<PlayerOne> ().dead)
     {
          StartCoroutine("RespawnPlayerOne");
     }
}

Also, your Object Find and GetComponent call are much more expensive than they should be, as they are called every frame. And you are potentially stacking Coroutines.

So i looked into object pooling and it works perfectly my friend thank you!