- Home /
How do I only clone 1 enemy instead of infinity enemies??
I made a script, and when the timer reaches 0 I want it to only spawn 1 enemy but it keeps spawning. What do I add to the script pls help.
var timer : float = 10.0; var enemy : Rigidbody; var sound : AudioClip;
function Update () {
timer -= Time.deltaTime;
if(timer <= 0)
{
var clone : Rigidbody;
clone = Instantiate(enemy, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward);
AudioSource.PlayClipAtPoint(sound, transform.position, 1);
}
}
Answer by saschandroid · Jun 22, 2016 at 06:59 AM
You could add a bool value to stop the spawning:
if ( timer <= 0 && spawned == false)
{
...
spawned = true;
}
Answer by Dibbie · Jun 22, 2016 at 07:09 AM
The reason why its continueing to spawn enimies is for 2 major factors.
You have this running in Update, so itll check your if-statement every frame.
Because of that, your if-statement itself, is checking if your timer is less or equal to 0... And you never reset your timer at any point, so it continues to count beyond zero, and any negative number is less or equal to zero...
You can fix this with 3 ways.
Create a variable, call it something like "allowSpawn" set it to true at the start. Once a enemy is spawned, after your audio line, add:
allowSpawn = false;
, now surround your entire if-statement in another if-statement that would read:if(allowSpawn == true){//your timer if-statement}
Change your if-statement to read
if(timer == 0)
instead, so only when it hits 0 it will spawn - realistically itll still continue to count down, but it wont continue to spawn things until it reaches 0 again, which it never will since you never reset the tmer.Reset your timer back to 10 seconds again, so after your audio line, youd add
timer = 10.0;
- now this means 1 enemy will spawn every 10 seconds... If you only want 1 enemy to spawn ever, after 10 seconds, use Invoke instead.
no 2 doesn't work ... timer almost never will be exactly zero
Then ins$$anonymous$$d of timer -= Time.deltaTime;
, use timer -= 1 * time.deltaTime;
, that way it removes 1 over the course 1 second, as an alternative. Though you could still try method 1 or 3.
I fixed it in this way
var timer : float = 10.0; var enemy : Rigidbody; var SpawnSound : AudioClip; var CountDownSound : AudioClip; var spawner : GameObject;
function Start () {
AudioSource.PlayClipAtPoint(CountDownSound, transform.position, 1);
}
function Update () {
timer -= Time.deltaTime;
if(timer <= 0)
{
var clone : Rigidbody;
clone = Instantiate(enemy, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward);
AudioSource.PlayClipAtPoint(SpawnSound, transform.position, 1);
}
if(timer <= -0.000000001) //change this number to change the amount of enemies spawned.
{
Destroy(spawner);
}
}
Your answer

Follow this Question
Related Questions
Make Fillamount Match Timer 1 Answer
Prefab clone not working 1 Answer
Subtract milliseconds from 12 hours? 1 Answer
Timer Countdown not subtracting value. 1 Answer
How to make an enemy chase player, using collisions 2 Answers