Respawn Time

Hey guys. I just have a quick question about respawning a character. I have a simple code worked out but I can’t get it to work with the WaitForSeconds() function. Here’s the code.

using UnityEngine;
using System.Collections;

public class spawnscript1 : MonoBehaviour {
	public GameObject playerr;  // the player that we have to respawn
	public GameObject ActualShip;  // the respawned character (the player's prefab)
	
	void Start () {
		}
		void Update(){
		playerr = GameObject.FindWithTag ("Player1");   // finding the player
		if (playerr == null){                    // if the player is dead (null)
		Instantiate (ActualShip, transform.position, transform.rotation);  // create a new player in this spot

I’ve tried working with creating an IEnumerator function and putting yield return new WaitForSeconds(3) and then the Instantiate function in it. (something like this)

void Start(){

if(playerr == null){
Spawn();
}
}

IEnumerator Spawn(){
yield return new WaitForSeconds(3f);
Instantiate(ActualShip,transform.position, transform.rotation);
}

Any and all help would be appreciated. Thanks!

you have a parcing error :stuck_out_tongue:

with IEnumerators you cant call it forth like a normal method.

you need to call it like this:

StartCoroutine ("Spawn");

//and in your IEnumerator:

IEnumerator Spawn(){
yield return new WaitForSeconds(3.0f);
Instantiate(ActualShip,transform.position, transform.rotation);
StopCoroutine ("Spawn"); // else the coroutine will keep looping even if your player is no longer null.
}