- Home /
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! :D
Please fix your code so that it is formatted correctly.
Answer by ChrisWoo · Jul 22, 2013 at 12:40 PM
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);
}
Answer by amphoterik · Jul 16, 2013 at 01:54 PM
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 still isn't working, but i have incorperated it into the code in the question. thanks for the help, and the quick response
i'm after the script constantly spawning the enemies, and not after they are killed.
O$$anonymous$$, I fixed it to do what you want now. It is now a recursive function that keeps running. You may want to add checks to end it if need be
Answer by inkspot · Jul 17, 2013 at 05:20 PM
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
}}
/// removes 1 second from the timer
Actually, this removes 1 every frame. To make it time relative, use Time.deltaTime :
RespawnDelay = RespawnDelay - Time.deltaTime;
Answer by RyanZimmerman87 · Jul 26, 2013 at 11:29 PM
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).
Your answer
Follow this Question
Related Questions
Help Needed With Object. 0 Answers
A respawn script for enemies. 1 Answer
How do I randomize the order of spawn points without repeats? 1 Answer
Respawning my AVATAR 1 Answer
the textures look bad 1 Answer