- Home /
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!
Note: If there is a parsing error or something you don't have to point that out. I wrote this code from the top of my head so I'm not really looking for answers like that.
Answer by Zentiu · Mar 02, 2014 at 04:04 PM
you have a parcing error :P
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.
}
Your answer
Follow this Question
Related Questions
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer
Player lives script 1 Answer
Respawn Player after Health 0 1 Answer
(Solved!) OnEnable spams error messages 1 Answer
Respawn System not Moving Character 0 Answers