Help! My respawn script isn't working! (Answered)

My respawn script isn’t working and i’m new to scripting, although I have watched alot of tutorials, so i have a a very faint idea of what i’m doing. Here is my script:

var Enemy : Transform;
var Spawn : Transform;
var RespawnTime : float = 60;
var RespawnDelay : float = 10;

function Start ()
{
    // Spawn enemy with RespawnTime as interval.
    InvokeRepeating("SpawnEnemy", 0, RespawnTime);
}
 
function SpawnEnemy()
{
   Instantiate(Enemy,Spawn.position,Spawn.rotation);
}

The problem is respawning. It’s supposed to spawn the enemy after a specific mount of time, but it simpily won’t spawn after the first time. Can anybody offer any insight or expertiese as to why it’s not working? Thanks for any replies in advance.

EDIT: thanks to everyone for their help and percistance! i’ve changed the script in the question to the one that works so that anyone in need of help in the future can use it. thanks again to everyone, and Chris Woo, who’s script was the right one! :smiley:

The problem is that you are only spawning the enemy when the script is first created (at the beginning of your game). You will also need a method to respawn the enemy after it dies. Basically, when the enemy dies, call this function (let’s call is “RespawnAfterDelay”) and let that recreate the enemy:

function Start()
{
    RespawnAfterDelay()
}

function RespawnAfterDelay()
{
    yield WaitForSeconds (RespawnDelay);
    Instantiate(Enemy,Spawn.position,Spawn.rotation);
    RespawnAfterDelay();
}

EDIT: fixed it to answer your question

This should work for you.
You can’t yield in Update.

var Enemy : Transform;
var Spawn : Transform;
var RespawnTime : float = 60;
var RespawnDelay : float = 10;

function Start ()
{
    // Spawn enemy with RespawnTime as interval.
    InvokeRepeating("SpawnEnemy", 0, RespawnTime);
}

function SpawnEnemy()
{
   Instantiate(Enemy,Spawn.position,Spawn.rotation);
}

Hello Voluntas!

What you need to do is call the function ‘function RespawnAfterDelay()’ after a period of time. for the effect you want. What you need to do is add something like a countdown(or countup) maybe this fast written down simple example will help you on your way to understand the concept;

function Update() {
if(RespawnDelay < 1) { /// check if the countdown reached 0
RespawnAfterDelay(); ///calls your spawn function
// reset your countdown e.g RespawnDelay = 60; //
} else {
RespawnDelay = RespawnDelay -1; /// removes 1 second from the timer
}}

Hmm this will be in C# since I never used JavaScript.

//make sure you set this to the right location
Transform spawn;

//timer for Ienumerator function
float timer;

//store your enemy prefab here
public GameObject enemyObject;

void Start()
{
timer = 60;

//calls first function to spawn first enemy and start timer for next one
firstEnemySpawnFunction();
}

void firstEnemySpawnFunction()
{
Instantiate(enemyObject, spawn.position, Quaternion.identity);

StartCoroutine(spawnEnemyFunction(timer));
}


IEnumerator spawnEnemyFunction(float timer)
{
yield return new WaitForSeconds(timer);
Instantiate(enemyObject, spawn.position, Quaternion.identity);

//could adjust timer her if you want for example
//timer = timer -1;

StartCoroutine(spawnEnemyFunction(timer));
}

I think that will work really easy for this kind of thing, you will just need to format it from C# to JavaScript (hopefully JavaScript has this functionality I’m not very familiar).