• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by emir3100 · Jun 22, 2016 at 05:59 AM · rigidbodyenemytimerclonetimer countdown

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);
 }


 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Dibbie · Jun 22, 2016 at 07:09 AM

The reason why its continueing to spawn enimies is for 2 major factors.

  1. You have this running in Update, so itll check your if-statement every frame.

  2. 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.

  1. 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}

  2. 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.

  3. 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saschandroid · Jun 22, 2016 at 07:14 AM 0
Share

no 2 doesn't work ... timer almost never will be exactly zero

avatar image Dibbie saschandroid · Jun 22, 2016 at 07:39 AM 0
Share

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.

avatar image emir3100 Dibbie · Jun 22, 2016 at 09:20 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Timer Progress Bar 2 Answers

UnityOSC and timer 0 Answers

How to prevent the use of other apps while my app is opened? - IOS How to block the use of certain apps? 0 Answers

How do I get my enemy to shoot my player? 1 Answer

Visual timer? Like CUT THE ROPE 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges